Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can someone help me with the correct formula to a function?
New Posts  All Forums:Forum Nav:

Can someone help me with the correct formula to a function?

post #1 of 5
Thread Starter 
Hey guys im using C++, i need a formula that calculate a several grades from the average using loops.

Now my project is working correctly with intended results however, i cannot get the difference from the average correctly.

Currently i have : difference = grades [i] - average, in a for loop. Is this enough information for any help?
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #2 of 5
Can we see the whole loop? Including the opening construct..

How is it not working? Are the numbers it's providing off? Is the compiler throwing an error? Is it crashing? etc etc
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #3 of 5
Thread Starter 
Quote:
Originally Posted by Plex View Post
Can we see the whole loop? Including the opening construct..

How is it not working? Are the numbers it's providing off? Is the compiler throwing an error? Is it crashing? etc etc

This is my whole source code:

#include <iostream>
#include <iomanip>
using namespace std ;

// Start functon prototype
float calcAverage(int[],int);
int calcMax(int[],int);
int calcDifference(int[],int,float);
void displayOutput(int[],int,float, int, int);
// End function prototype

// Start Main
int main(void)
{
//Local Declarations
const int NUM = 10 ;
int i = 0, grades[NUM], difference = 0,max = 0 ;
float average = 0 ;

// Program Statement
for(i = 0; i < NUM; i++)
{
cout << " Please enter a grade: " ;
cin >> grades[i] ;
cout << "" << endl ;
}
cout << "" << endl;

average = calcAverage(grades,NUM) ;
max = calcMax(grades,NUM) ;
difference = calcDifference(grades,NUM,average);
displayOutput(grades,NUM,average,difference,max) ;
return 0 ;
}
// End Main


// Start calcAverage function
float calcAverage( int grades[], int NUM)
{
// Local Declarations
float average = 0, sum = 0 ;
int i ;

// Program Statement
for( i = 0; i < NUM; i++)
{
sum = sum + grades[i] ;
average = sum/NUM ;
}
return average ;
}
// End calcAverage function

// Start calcMax function
int calcMax(int grades[], int NUM)
{
// Local Declaration
int i = 0, max = grades[0] ;

// Program Statement
for( i = 0; i < NUM; i++)
{
if(max < grades[i])
max = grades[i] ;
}
return max ;
}
// End calcMax function

// Start calcDifference
int calcDifference(int grades[],int NUM, float average)
{
// Local variables
int i = 0, difference = 0;
// Program Statement
for(i = 0; i < NUM; i++)
{
difference = grades[i] - average ;
}
return difference ;
}


// Start displayOutput function
void displayOutput(int grades[], int NUM, float average, int difference, int max)
{
// Local Declarations
int i = 0 ;

// Program Statement
cout << " GRADE AVERAGE Difference from the Average "<<endl
<< " ===== ======= =========================== "<<endl;

for (i = 0; i < NUM; i++)
cout << setw(6)<<right<< grades[i]<< " "
<< setw(7)<<right<< average << " "
<< setw(25)<<right<< difference << endl
<< endl ;

cout<< " Highest Grade = " << max << endl ;




return ;
}

Now i do not get syntax errors, the program runs as inteded but i do not get the values i want in the Difference from Average heading. I figured it had to be a logical error, perhaps I am missing something, or simply need to change something minor.
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #4 of 5
Well, you need to take a second look at the calcDifference function and figure out what's going on and why it's displaying the same difference for every single grade.

Hint: You're getting the difference for each element, but you're only returning the last elements' difference. So, you need to figure out a way to store each elements' difference from the average rather than storing only the last elements' difference from the average.
post #5 of 5
I've created a small version of the program you were assigned. Take a look at the code and try to understand what is happening. Then, implement it into your code. Feel free to take as much of it as you need, bud.

Code:
#include <iostream>
#include <iomanip>
using namespace std;

#define arraySize 3

double calculateAverage(int data[]);
void calculateDifference(int data[], double average);
void outputDifference(int data[], int index, double average);

int main()
{
    int numbers[arraySize] = {4, 8, 11};
    calculateDifference(numbers, calculateAverage(numbers));

    return 0;
}

double calculateAverage(int data[])
{
    double average = 0.0;
    int count = 0;
    double sum = 0.0;

    for(int i = 0; i < arraySize; i++)
    {
        sum += data[i];
        count++;
    }

    average = sum / count;

    return average;
}

void calculateDifference(int data[], double average)
{
    cout << setw(8)  << "Grades"
         << setw(16)  << "Average"
         << setw(25) << "Difference"  << endl;
    for(int i = 0; i < arraySize; i++)
    {
        outputDifference(data, i, average);
    }
}

void outputDifference(int data[], int index, double average)
{
    cout << setw(8)  << data[index]   << " "
         << setw(16)  << average     << " "
         << setw(25) << data[index] - average  << endl;
}
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can someone help me with the correct formula to a function?