Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 09-29-09   #1 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 438

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default Importing from one function to another?

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;

}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 09-29-09   #2 (permalink)
µaestro
 
RonindeBeatrice's Avatar
 
intel ati

Join Date: Mar 2006
Location: Virginia, USA
Posts: 7,432
Blog Entries: 1

FAQs Submitted: 1
Folding Team Rank: 158
Trader Rating: 50
Default

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:
Originally Posted by pow3rtr1p View Post
Can't wait for the completed build. It does look like Pure Action to me. It's tall and dark and thick, and you mounted those very well.
-Overclock.net Professionalism Initiative

System: Those Jones need to stand still
CPU
i7 920 C0
Motherboard
Asus Rampage II Gene
Memory
6GB OCZ DDR31600 CL7
Graphics Card
Sapphire HD4850 1GB
Hard Drive
WD 640GB AAKS
Power Supply
Enermax Infiniti 720
Case
Soldam Windy Altium FC-300 MB "Fireball"
CPU cooling
Noctua NH-U12P
OS
Win 7 RC
Monitor
HP w2408
Overclock.net - 2009 Chimp Challenge Champions 1 Million+ Folding at Home points

Last edited by RonindeBeatrice : 09-29-09 at 11:04 PM
RonindeBeatrice is offline I fold for Overclock.net Overclocked Account RonindeBeatrice's Gallery   Reply With Quote
Old 09-29-09   #3 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 438

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default

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;

}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit

Last edited by godsgift2dagame : 09-29-09 at 11:41 PM
godsgift2dagame is offline   Reply With Quote
Old 09-30-09   #4 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

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;
}
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 09-30-09   #5 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 438

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default

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
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 10-01-09   #6 (permalink)
Programmer
 
lordikon's Avatar
 
intel nvidia

Join Date: Feb 2008
Location: Denver, CO
Posts: 3,930

Rep: 191 lordikon is acknowledged by manylordikon is acknowledged by many
Unique Rep: 153
Folding Team Rank: 104
Trader Rating: 0
Default

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;
}
2.) Example #1 above is pretty much worthless, it multiplies my number by two, but then I don't get that number back to do anything with it. Functions can return value(s). So to expand on example #1, I will have an integer that is multiplied by two, and I want that number back, so I have to return an integer.

Example #2:
Code:
// Declaration
int MulByTwo( int myNumber );

// Definition
int MulByTwo( int myNumber )
{
    myNumber *= 2;
    return myNumber;  // This is returned
}
3.) So now I need to be able to use my function for other things, like multiplying a number by two so I can pass that number to another function.

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
}
Notice that 'SomeOtherFunction' takes an 'int' as a parameter, this allows me to pass it 'myNumber' which is of the type int.
__________________
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

System: Max Pwnage
CPU
e8400 E0, 3.81Ghz, 1.324v
Motherboard
Asus P5N-D 750i
Memory
4GB (2x2gb) A-Data DDR2-800
Graphics Card
2x 8800GTS 512(G92) SLI
Hard Drive
150 WD Raptor, 640 WD Ext.
Power Supply
750W PC P&C Silencer
Case
CoolerMaster 690
CPU cooling
$7USD Copper HS
GPU cooling
Stock
OS
Vista Home Premium
Monitor
22" Asus AL2223W
1 Million+ Folding at Home points
lordikon is offline I fold for Overclock.net   Reply With Quote
Old 10-04-09   #7 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 438

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default

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);
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 10-04-09   #8 (permalink)
a.k.a. TutenStain
 
i_ame_killer_2's Avatar
 
intel nvidia

Join Date: Jun 2006
Location: Sweden
Posts: 4,778

Rep: 624 i_ame_killer_2 is becoming famousi_ame_killer_2 is becoming famousi_ame_killer_2 is becoming famousi_ame_killer_2 is becoming famousi_ame_killer_2 is becoming famousi_ame_killer_2 is becoming famous
Unique Rep: 360
Trader Rating: 0
Default

Code:
  for(i = 0; i < 5; i++)
    {
    cout << "Enter score: "; 
    cin >> numbers;
    }
In this loop the number will be rewritten every time. I guess thats not your intention as you want to find the lowest number? You need to store the numbers in some way like an array OR use rabidgnome229 method in this thread: http://www.overclock.net/coding-prog...tinel-c-2.html

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

System: The Kandalf
CPU
Q6600 [G0] 3.2Ghz @ 1.320
Motherboard
MSI P45 Platinum
Memory
Crucial BallistiX PC 6400 2GB
Graphics Card
Gainward GTS 250 512MB
Hard Drive
Samsung SpinPoint F1 1TB
Sound Card
ALC 888
Power Supply
Corsair VX450W
Case
Thermaltake Kandalf
CPU cooling
Tuniq Tower 120
GPU cooling
Stock Dual-slot cooler
OS
Windows 7 Ultimate 64 Bit
Monitor
22" Samsung Syncmaster 226CW + 15" Compaq 1501

Last edited by i_ame_killer_2 : 10-04-09 at 01:38 PM
i_ame_killer_2 is online now Overclocked Account i_ame_killer_2's Gallery   Reply With Quote
Old 10-04-09   #9 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 438

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default

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;                           
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Old 10-05-09   #10 (permalink)
AMD Overclocker
 
amd nvidia

Join Date: Apr 2009
Posts: 438

Rep: 19 godsgift2dagame Unknown
Unique Rep: 17
Trader Rating: 0
Default

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;
}
__________________
System: $775
CPU
Phenom II 720
Motherboard
Gigabyte MA790X-UD4P
Memory
4GB OCZ Reaper DDR2 1150
Hard Drive
250GB Seagate Barracuda 7200.10 x 2
Power Supply
PC Power & Cooling 610W
Case
Antec 300
CPU cooling
XIGMATEK Dark Knight-S1283V
OS
Vista 64-bit
godsgift2dagame is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 04:31 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright İ 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.15894 seconds with 8 queries