|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Importing from one function to another?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||
|
AMD Overclocker
|
Hey guys,
__________________I have one function GetValues that lets user input the test scores. However, I then want to have the function FindLowest find the lowest score, but can't seem to manage to "import" the function and its entered data to the function...could someone please help? Code:
#include <iostream>
using namespace std;
int GetValues();
int FindLowest();
int CalcAverage();
int main(){
GetValues();
FindLowest();
return 0;
}
int GetValues(){
int score, grades[5]={0};
double sum;
for (int i = 0; i < 5; i++)
{
grades[i] = score;
cout << "Enter score ";
cin >> score;
fflush(stdin);
sum += score;
}
return 0;
}
int FindLowest(){
GetValues();
int min = 101;
int low;
if(grade[1] < min)
grade[1] = low;
cout << "The lowest number is " << low;
getchar();
getchar();
return min;
}
|
|||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
µaestro
![]() |
I just tried to recall C and my head exploded.
Basically, you want to declare your array inside FindLowest, then pass the array into a call to getvalues as a reference(*). When getValues finishes up the values will be sitting pretty in FindLowest waiting to get worked.
__________________
Quote:
Last edited by RonindeBeatrice : 09-29-09 at 11:04 PM |
||||||||||||
|
|
|
|
#3 (permalink) | |||||||||
|
AMD Overclocker
|
Could you please give me an idea how? I've tried doing what you said in the way I understood you meant it, but I can't for the life of me, figure it out.
__________________EDIT: This is what I did. Code:
int FindLowest(){
int* GetValues();
int min = 101;
int low;
if(grades[1] < min)
grades[1] = low;
cout << "The lowest number is " << low;
getchar();
getchar();
return min;
}
Last edited by godsgift2dagame : 09-29-09 at 11:41 PM |
|||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
With great difficulty
![]() |
You just need to pass the values between different functions. Here's an example of a program somewhat like yours. It has one function that fills an array with values, and one that performs a calculation on those values (finds the average). You can use this as a reference on how to pass data between functions
__________________Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;
using std::endl;
void fillArray(int *array, int num){
srand(time(NULL));
for(int i = 0; i < num; ++i)
array[i] = rand() % 100;
}
void processArray(int *array, int num){
int sum = 0;
for(int i = 0; i < num; ++i)
sum += array[i];
float avg = (float)sum / (float)num;
cout << "The average of the " << num << " given values is " << avg << endl;
}
int main(void){
const int n = 5;
int values[n];
fillArray(values, n);
processArray(values, n);
return 0;
}
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||
|
AMD Overclocker
|
I'm still not getting it to work. I got real close during class today, but forgot to send myself a copy for home. DAMN
__________________
|
|||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||||
|
Programmer
![]() |
Today's lesson, functions:
1.) If you want to pass something to a function, you must pass it a type(s) as parameters. Example #1, If I want a function that I pass an integer, and it multiplies that by two, I could do something like this: Code:
// Declaration
void MulByTwo( int myNumber );
// Definition
void MulByTwo( int myNumber )
{
myNumber *= 2;
}
Example #2: Code:
// Declaration
int MulByTwo( int myNumber );
// Definition
int MulByTwo( int myNumber )
{
myNumber *= 2;
return myNumber; // This is returned
}
Example #3 Code:
void SomeOtherFunction( int otherNumber );
int main()
{
int myNumber = 10;
myNumber = MulByTwo( myNumber ); // Now 10 becomes 20
SomeOtherFunction( myNumber ); // My other function can use this
}
__________________
Current Modern Warfare 2 petition count: http://sebastien.me/mw2/petition.png Currently folding with: 2 8800GTS g92s -- 1 GTX 275 -- 1 8600GTS -- 1 e8400 -- 1 i7 950 (50%) -- 1 e6600
|
||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||
|
AMD Overclocker
|
Am I missing something...?
__________________Code:
#include <iostream>
using namespace std;
int getnumbers(int numbers);
int findlowest(int numbers, int min);
int getnumbers()
{
int numbers;
int i = 0;
for(i = 0; i < 5; i++)
{
cout << "Enter score: ";
cin >> numbers;
}
return numbers;
}
int findlowest(int numbers)
{
int min = 101;
if(numbers < min)
min = numbers;
return min;
}
int main()
{
getnumbers();
findlowest(int numbers, int min);
}
|
|||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
a.k.a. TutenStain
![]() |
Code:
for(i = 0; i < 5; i++)
{
cout << "Enter score: ";
cin >> numbers;
}
It seems that you are not familiar with functions by looking at your code. I would recommend reading this: http://www.cplusplus.com/doc/tutorial/functions/ Also, it might be easier to just write your code w/o functions and then write it again but with functions by looking at your code that you wrote. In this way you are at least sure that your base code is OK.
__________________
Please ignor my spelling and grammar! FAQ:s I reject your reality and substitute my own. -Adam Savage
Last edited by i_ame_killer_2 : 10-04-09 at 01:38 PM |
|||||||||||||
|
|
|
|
#9 (permalink) | |||||||||
|
AMD Overclocker
|
Base Code
__________________Code:
#include <iostream>
using namespace std;
int GetValues();
int FindLowest();
int CalcAverage();
int main(){
double num1, num2, num3, num4, num5;
int sum;
int min = 101;
double average;
//Get Values
cout << "Enter first grade ";
cin >> num1;
cout << endl;
cout << "Enter second grade ";
cin >> num2;
cout << endl;
cout << "Enter third grade ";
cin >> num3;
cout << endl;
cout << "Enter fourth grade ";
cin >> num4;
cout << endl;
cout << "Enter fifth grade ";
cin >> num5;
sum = num1 + num2 + num3 + num4 + num5;
cout << "The sum is " << sum << endl;
//Find smallest number and delete
if(num1 < min)
min = num1;
if(num2 < num1)
min = num2;
if(num3 < num2)
min = num3;
if(num4 < num3)
min = num4;
if(num5 < num4)
min = num5;
cout << "The lowest score, (" << min << ") will be discarded in the calculations." << endl;
//Find Average
average = (num1 + num2 + num3 + num4 + num5 - min) / 4;
cout << "The average, after the worst score was dropped, is " << average << "%." << endl;
getchar();
getchar();
return 0;
}
|
|||||||||
|
|
|
|
|
#10 (permalink) | |||||||||
|
AMD Overclocker
|
Got it done...for the most part...only issue to resolve is getting the average to show a decimal.
__________________Code:
#include <iostream>
using namespace std;
int GetValues(int num[], int x);
int GetLowest(int num[], int x);
int GetAverage(int sum, int min);
int main()
{
int num[5] = {0};
int sum = GetValues(num,5);
int min = GetLowest(num,5);
double total = GetAverage(sum, min);
getchar();
getchar();
return 0;
}
int GetValues(int num[], int x = 5)
{
int sum = 0;
for(int i = 0; i < x; i++)
{
cout << "Enter numbers ";
cin >> num[i];
sum += num[i];
}
cout << endl << "The sum of the numbers is " << sum << endl << endl;
return sum;
}
int GetLowest(int num[], int x)
{
int min = 101;
if(num[0] < min)
{
min = num[0];
}
if(num[1] < num[0])
{
min = num[1];
}
if(num[2] < num[1])
{
min = num[2];
}
if(num[3] < num[2])
{
min = num[3];
}
if(num[4] < num[3])
{
min = num[4];
}
cout << "The minimum number, " << min << ", will be discarded from the remaining calculations." << endl;
return min;
}
int GetAverage(int sum, int min)
{
double total;
total = (sum - min) / 4;
cout << "The average is " << total;
return 0;
}
|
|||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|