-
logic error
This program is receive upto 25 grades and average them and display the results. It is in the required format, but there is atl least one logic error that I am not seeing. any help would be greatly appreciated.
Thanks Steve
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
int getinput(float array[]);
void mainmenu();
float Calculateaverage ( float array[], int count);
void Displayarray( float array [], float avg, int count);
main()
{
int choice, count;
float avg;
float array[25] = {0};
do
{
mainmenu();
cin >> choice;
if ( choice == 1 )
{
count = getinput ( array );
avg= Calculateaverage( array, count);
Displayarray( array, avg, count);
}
else
break;
}while ( choice !=2 );
}
/************************************************** **********************
thepurpose of this function is to format the screen and present the user
with options.
************************************************** **********************/
void mainmenu()
{
// system ("cls");
cout<<"1-Enter Numbers"<<endl;
cout<<"2-Exit "<<endl;
}
/************************************************** ********************
The pupose of this function is to receive input from the user and pass
back the necessassary information.
************************************************** ********************/
int getinput(float array[])
{
float num;
int s;
cout<<"if you wish to exit before entering all 25 numbers enter 999"<<endl;
for (s=0;s<25;s++)
{
cin>> num;
if (num == 999)
break;
else
array[s] = num;
}
return s;
}
/************************************************** ********************
the purpose of this function is to receive pertnet information and
generate and average, and pass it back to main;
************************************************** *******************/
float Calculateaverage( float array[], int count)
{
int s;
float sum, avg;
for (s=0;s<count;s++)
{
sum += array[s];
}
cout<<"we are in calcavg"<<endl;
avg= (sum / count);
return avg;
cout<<avg<<endl;
}
/************************************************** ******************
The pupose of this function is to display the results to the screen.
************************************************** *******************/
void Displayarray( float array[], float avg, int count)
{
int s;
for (s=0;s<count;s++)
{
cout<<array[s]<<endl;
cout<<avg<<endl;
}
}
-
wow...what have you done :confused: my suggestions are as follows :
1. mainmenu should be returning choice to main. you are trying to cin choice in main....not possible!
2. getinput can do the same thing..that is return each value entered by the user and be put into the array in main e.g
Code:
int val=0, count=0, i=0;
while ( i<25 && val != 999)
{
val=getinput();
if (val != 999)
{
array[i]=val;
count++;
}
}
this way you will fall from the loop if either i >= 25 or if val==999
and 999 will not be put into the first position of your array.
if you look at your code and following each action step by step you will realise where your problems are.
good luck
-
will this work. i nixed some of your functions (for me they're more burdensome than it's worth). while sweets' calc methode is easier to understand, you can still use a function to manipulate the array. don't forget to use code tags next time (and maybe brush up on your english if you want others to understand you :) )
Code:
#include <iostream.h>
void f_getinput(float array[], int size);
//void, don't return anything, cbelow
int f_mainmenu();
main()
{
int choice, i;
float avg, array[25] = {0};
do
{
choice=f_mainmenu(); //what sweets said
if ( choice == 1 )
{
f_getinput (array, 25);
//i think you need the array size for the call-by-ref
//i couldn't understand what you were doing with that calc stuff,
//will this work? :)
for (i=0; i<25; i++)
{
avg += array[i];
}
avg /= 25;
cout<<"avg is "<<avg<<endl<<"scores are:"<<endl;
for (i=0; i<25; i++)
{
cout<<array[i]<<endl;
}
}
else
break;
}while ( choice !=2 );
}
int f_mainmenu()
{
int x;
cout<<"1-Enter Numbers"<<endl;
cout<<"2-Exit "<<endl;
cin>>x;
return x;
}
void f_getinput(float array[], int y)
{
float num;
int s;
cout<<"if you wish to exit before entering all 25 numbers enter 999"<<endl;
for (s=0;s<y;s++)
{
cin>> num;
if (num == 999)
break;
else
array[s] = num;
//that's all don't return anything,
//in call-by-ref you're changing the actual array
}
}
-
just rushed thru your code and saw this
in the display function you are displaying the avg 25 times along with the numbers, maybe thats the logical error
-
blight2c.....I have always believed that students should learn through the process of trial and error. by giving steve the actual program will not help to make him a better programmer. he must learn for himself with his own techniques.
sure getting help along the way is good but I dont think that we should be re-writting their programs. Im sure you only meant to help him but the best help that you, I and everyone else can give to someone who is learning a program, is by showing them alternate directions to their problems....giving them the solution is not the answer.
sophie
-
thnaks for the help
Alot of you think that not giving out the answer is the best way, but pointing some one in the right direction isn't giving them the answer. I foound my error when I went back to the basics. Granted it is a mistake that I am not likly to make again, but I think when someone has written the program and knows enough
to say that it is a logic error in this part of the process, It would not be a bad thing to point them in the right direction. ie.... having told me to initialize my varibles. Just my thoughts. Thanks for the help of those who tried eventhough they faced persicution for it.
Steve