Here is my code. I have a warning that
(127) : warning C4715: 'ComputeSum' : not all control paths return a value

#include <iostream>

using namespace std;

const int MAX = 10;

// Function prototypes, i.e., declarations for the functions we have created

void ReadInput (int A[], int number, int i);

void ProcessData (int A[]);

double ComputeSum (int sum, int A[]);

void ComputeMean (double mean, int sum);

void PrintData (int sum, double mean);

double mean (int sum, int i) ;
   // compute an average, given a sum and the amount of numbers

// **************************************************************************



//Main Module

int main()

{
   // variable definitions

   char proceed ; // start collecting data? coded y/n
   int sum ; // declare sum as integer
   double mean; // declare mean as a double
   int i ; // declare i as integer
   int number; //declare number as integer
   
   // initialize variables

  
   i = 0;
   number = 0;
   sum = 0 ;
   mean = 0;
   int A[MAX] ; // declare the size of the array

   cout << "This program computes the average of 10 numbers" ;
   cout << endl ;
   cout << "Do you want to start data entry now ? (y/n)" ;
   cin  >> proceed ;
   cout << endl ;

   while (proceed == 'y')


{

      ReadInput (A, number, i);

      ProcessData (A);

   } 

ComputeSum (sum, A);

ComputeMean (mean, sum);

PrintData (sum, mean);

return 0;
}

//Input Module (A)
void ReadInput (int A[10], int number, int i)
{
   for ( i = 0; i < MAX; i++) //create for loop
   {
	  cout << "Enter a positive number:" ;
      cin >> number ;
   }
}


void ProcessData (int A[MAX], int number, int i)
{
   if (number <= 0) // if else statement (INSIDE the loop) to calculate for user error
   {
         cout << "Invalid Entry! Enter a number greater than zero." ;
   }
      else if (number > 0)
	  {
         A[i] = number ;
   cout << endl;
   }
}


// Calculation Module (sum, mean)

double ComputeSum (int sum, int A[])
{
for (int i = 0; i < MAX; i++)

{sum = sum + A[i] ;
return sum;
}
}

void ComputeMean (double mean, int sum)
{
{
   mean = 1.0 * sum / MAX;
}
cout << endl ;
}

// Output Module (sum, mean)

void PrintData (int sum, double mean)
{
cout << "The sum is" << sum << endl ;
cout << "The average is" << mean << endl ;
}

Recommended Answers

All 18 Replies

put the return outside the for loop, otherwise you will exit the loop the first time through every time it's called.

Consistent indenting would improve your code even better than the appropriate placement of the return value in computeSum(), however.

when I compile it has no errors, but when i try to run the program it has 2 errors.

Valved Final Project code.obj : error LNK2019: unresolved external symbol "void __cdecl ProcessData(int * const)" (?ProcessData@@YAXQAH@Z) referenced in function _main
C:\Documents and Settings\daniel goodwin\My Documents\school\CMIS 102\Valved Final Project\Debug\Valved Final Project.exe : fatal error LNK1120: 1 unresolved externals
Build log was saved at "file://c:\Documents and Settings\daniel goodwin\My Documents\school\CMIS 102\Valved Final Project\Valved Final Project\Debug\BuildLog.htm"
Valved Final Project - 2 error(s), 0 warning(s)

This is one of the most crazy style of programming which i have ever seen...
ok let me make it in point wise


1. What kind a for variable the mean is?

void ComputeMean (double mean, int sum)
{
{
   mean = 1.0 * sum / MAX;
}
cout << endl ;
}

>>Sounds like crazy. :D In this case the passing of argument has not use at all.
>>what with you why are you using one more {} parenthesis what you are trying to prove by doing that.
>>At least if you wanted to make it function work you must possible passed the reference to the function like

void ComputeMean(double &mean, int sum);

:D
>> Using my imagination code possibly might be

double ComputeMean(int sum)
{ return  1.0* sum/MAX; }

2. The return type is double and you are returning int what does it mean?

double ComputeSum (int sum, int A[])
{
for (int i = 0; i < MAX; i++)

{sum = sum + A[i] ;
return sum;
}
}

>>You must see the for loop will just have one cycle what ever you do. :D
>>What the problem with the sum variable doesn't make scene at all.
>>OK. I you really want the same code to run so badly then. It might look some how like this.

int ComputeSum (int sum, int A[])
{
 return sum + A[i] ;;
}

3. Oh.. boy my head this started aching seeing your code.. ok but let me tell you as good tip at last..
>> It might be good technique to add and compute the no as you take it.
>> now i'm really going down.:ooh:

i've made changes to the code and now the program runs. after putting in ten number (over 0) it says "Invalid Entry! Enter a number greater than zero."

#include <iostream>

using namespace std;

const int MAX = 10;

// Function prototypes, i.e., declarations for the functions we have created

void ReadInput (int A[], int number, int i);

void ProcessData (int A[MAX], int number, int i);

double ComputeSum (int sum, int A[]);

void ComputeMean (double mean, int sum);

void PrintData (int sum, double mean);

double mean (int sum, int i) ;
   // compute an average, given a sum and the amount of numbers

// **************************************************************************



//Main Module

int main()

{
   // variable definitions

   char proceed ; // start collecting data? coded y/n
   int sum ; // declare sum as integer
   double mean; // declare mean as a double
   int i ; // declare i as integer
   int number; //declare number as integer
   
   // initialize variables

  
   i = 0;
   number = 0;
   sum = 0 ;
   mean = 0;
   int A[MAX] ; // declare the size of the array

   cout << "This program computes the average of 10 numbers" ;
   cout << endl ;
   cout << "Do you want to start data entry now ? (y/n)" ;
   cin  >> proceed ;
   cout << endl ;

   while (proceed == 'y')


{

      ReadInput (A, number, i);

      ProcessData (A, number, i);

   } 

ComputeSum (sum, A);

ComputeMean (mean, sum);

PrintData (sum, mean);

return 0;
}

//Input Module (A)
void ReadInput (int A[10], int number, int i)
{
   for ( i = 0; i < MAX; i++) //create for loop
   {
	  cout << "Enter a positive number:" ;
      cin >> number ;
   }
}


void ProcessData (int A[MAX], int number, int i)
{
   if (number <= 0) // if else statement (INSIDE the loop) to calculate for user error
   {
         cout << "Invalid Entry! Enter a number greater than zero." ;
   }
      else if (number > 0)
	  {
         A[i] = number ;
   cout << endl;
   }
}


// Calculation Module (sum, mean)

double ComputeSum (int sum, int A[])
{
for (int i = 0; i < MAX; i++)

sum = sum + A[i] ;

return sum;

}

void ComputeMean (double mean, int sum)
{
{
   mean = 1.0 * sum / MAX;
}
cout << endl ;
}

// Output Module (sum, mean)

void PrintData (int sum, double mean)
{
cout << "The sum is" << sum << endl ;
cout << "The average is" << mean << endl ;
}

Hi,
mmmmm Can it be that u kind of make things complicated???
Have a look at the folowing code, that makes the same like yours and tell me your conclusions :D

#include <iostream>

int main()
{
	int sum=0;			//The sum
	double mean= 0;		//The mean
	int number[10];		//The array....
	
	std::cout << "This program computes the sum and  " << std::endl ;
	std::cout << "the average of 10 numbers using an array." <<std::endl;
	for(int n=0;n<10;++n)
	{
		std::cout<< "Enter the " <<n+1<<" number"<<std::endl;
		std::cin >> number[n];
		sum+=number[n];	
	}
	mean=sum/10.0;
	std::cout << "The sum is = " << sum << std::endl ;
	std::cout << "The average is =" << mean << std::endl ;
}

Hi,
mmmmm Can it be that u kind of make things complicated???
Have a look at the folowing code, that makes the same like yours and tell me your conclusions :D

#include <iostream>

int main()
{
	int sum=0;			//The sum
	double mean= 0;		//The mean
	int number[10];		//The array....
	
	std::cout << "This program computes the sum and  " << std::endl ;
	std::cout << "the average of 10 numbers using an array." <<std::endl;
	for(int n=0;n<10;++n)
	{
		std::cout<< "Enter the " <<n+1<<" number"<<std::endl;
		std::cin >> number[n];
		sum+=number[n];	
	}
	mean=sum/10.0;
	std::cout << "The sum is = " << sum << std::endl ;
	std::cout << "The average is =" << mean << std::endl ;
}

you missed the return statements :)

I can't simplify the code that much. It has to be seperate funtions that are called into main. It has to have an array. Please help

I can't simplify the code that much. It has to be seperate funtions that are called into main. It has to have an array. Please help

ok ok wait for 30 min

you missed the return statements

...Its kind of late and i am sleepy :zzz: but when I wake up tomorrow I will ...return 0; :D :D

I can't simplify the code that much. It has to be separate functions that are called into main. It has to have an array. Please help

Could u post the exact assignment description??

I guess rest you can figure it out

#include<iostream>
using namespace std;

#define cls system("cls"); //for clear screen its my style
#define max 10

int index=0;
int no[max];
int sum=0;
float mean;

void input(){  cout<<"Enter any no: "; cin>>no[index++]; }
void sum() { for(int i=0; i<max; i++) sum+=no[i];}
void mean() {mean=sum/index;}
void display() {cout<<"\nSum: "<<sum<<"\nMean: "<<mean;}

void menu();

int main()
{
 menu();
 return 0;
}

void menu()
{
 for(;;)
      {
        cls;
        cout<<"Crazy stuff\n"
              <<"\n1. Input data"
              <<"\n2. Sum it"
              <<"\n3. Mean"
              <<"\n4. Display"
              <<"\n Are you crazy what is your choice: ";
       switch(getch())
            {
              case '1': cls; input(); getch(); break;
              case '2': cls; sum(); getch(); break;
              case '3': cls: mean(); getch(); break;
              case '4': cls; display(); getch(); break;
              case 27: /*you press Esc to Escape*/ return;
            }
}
}

Or since I started it .... if u prefer:

#include <iostream>

const int maxNum(10);
int Numbers[maxNum];

bool ReadInput (int []);
int calculateSum (int []);
double CalculateMean (const int& );
void PrintData (int ,double);

int main()
{
	int sum = 0;			//The sum
	double mean = 0;		//The mean

	std::cout << "This program computes the sum and the average of " << std::endl ;
	std::cout << "10 positive numbers using an array. (To exit enter -1)" <<std::endl;
	if (ReadInput(Numbers))
	{
		sum=calculateSum(Numbers);
		mean=CalculateMean(sum);
		PrintData(sum,mean);
	}
	return 0;
}
//reading the input numbers
bool ReadInput (int Numbers [maxNum])
{
	int number = 0;
	for (int i = 0; i < maxNum; ++i) //create for loop
	{
		std::cout << "Enter number "<<i+1<<" of the 10 numbers:" ;
		std::cin >> number ;
		if (number<-1)
		{
			std::cout<<"Ups Negative number entered!! Ciao"<<std::endl;
			return false;
		}
		else if (number==-1)
		{
			std::cout<<"Ciao"<<std::endl;
			return false;
		}
		else 
		{
			Numbers[i]=number;
			return true;
		}
	}
}
//Calculate sum
int calculateSum (int Numbers [maxNum])
{
	int sum=0;
	for (int i = 0; i < maxNum; ++i) //create for loop
	{
		sum+=Numbers[i];
	}

	return sum;
}

//Calculate mean
double CalculateMean (const int& sum)
{
	return sum/10.0;
}

// Output Module (sum, mean)
void PrintData (int sum, double mean)
{
	std::cout << "The sum is = " << sum << std::endl ;
	std::cout << "The average is = " << mean << std::endl ;
}

I tried your code and it says ReadInput: not all control paths return a value

Yes u r right, but its only a warning :d

I tried your code and it says ReadInput: not all control paths return a value

Here is the same corrected (no warnings):

#include <iostream>

const int maxNum(10);
int Numbers[maxNum];

bool ReadInput (int []);
int calculateSum (int []);
double CalculateMean (const int& );
void PrintData (int ,double);

int main()
{
	int sum = 0;			//The sum
	double mean = 0;		//The mean

	std::cout << "This program computes the sum and the average of " << std::endl ;
	std::cout << "10 positive numbers using an array. (To exit enter -1)" <<std::endl;
	if (ReadInput(Numbers))
	{
		sum=calculateSum(Numbers);
		mean=CalculateMean(sum);
		PrintData(sum,mean);
	}
	return 0;
}
//reading the input numbers
bool ReadInput (int Numbers [maxNum])
{
	int number = 0;
	bool result;//added
	for (int i = 0; i < maxNum; ++i) //create for loop
	{
		std::cout << "Enter number "<<i+1<<" of the 10 numbers:" ;
		std::cin >> number ;
		if (number<-1)
		{
			std::cout<<"Ups Negative number entered!! Ciao"<<std::endl;
			result = false;  //changed
		}
		else if (number==-1)
		{
			std::cout<<"Ciao"<<std::endl;
			result =  false; //changed
                        break;         //Added -WITHOUT IT -> A BUG
		}
		else 
		{
			Numbers[i]=number;
			result =  true; //changed
		}
	}
	return result;  //added
}
//Calculate sum
int calculateSum (int Numbers [maxNum])
{
	int sum=0;
	for (int i = 0; i < maxNum; ++i) //create for loop
	{
		sum+=Numbers[i];
	}

	return sum;
}

//Calculate mean
double CalculateMean (const int& sum)
{
	return sum/10.0;
}

// Output Module (sum, mean)
void PrintData (int sum, double mean)
{
	std::cout << "The sum is = " << sum << std::endl ;
	std::cout << "The average is = " << mean << std::endl ;
}

However u should have it found it yourself :D :D
----
There are many ways to produce working code I think.
I prefer personally to stick it some principles:
-use standard c++
-use only C++ features , and not C (e.g scanf e.t.c)
-use OOP whenever possible because this the reason of the c++ creation regarding the fact that there was C already.
-keep code simple
-keep code readable
----

I tried that but it had 3 errors. Here is my new code. It runs but exits after putting in 10 numbers and never gives the sum or average. The teacher suggested to add print statements at various points in your program to make sure values are being transferred properly. I'm not sure how to do this.

/*******************************************************/
/*  File: Final Project                                */
/*                                                     */
/* Program to compute the average of ten integers      */
/*                                                     */
/* Inputs: (keyboard)                                  */
/* 1. ten numbers - negative numbers not allowed       */
/*                                                     */
/* Output:                                             */
/* Display the sum and average of the numbers input    */
/* using a constant for the array size                 */
/* and computing the average of all the elements       */
/* in the array                                        */
/*******************************************************/
 

#include <iostream>

using namespace std;

const int MAX = 10;

// Function prototypes, i.e., declarations for the functions we have created

void ReadInput (int A[], int number, int i);

void ProcessData (int A[MAX], int number, int i);

double ComputeSum (int sum, int A[]);

void ComputeMean (double mean, int sum);

void PrintData (int sum, double mean);

double mean (int sum, int i) ;
   // compute an average, given a sum and the amount of numbers

// **************************************************************************



//Main Module

int main()

{
   // variable definitions

  
   int sum ; // declare sum as integer
   double mean; // declare mean as a double
   int i ; // declare i as integer
   int number; //declare number as integer
   
   // initialize variables

  
   i = 0;
   number = 0;
   sum = 0 ;
   mean = 0;
   int A[MAX] ; // declare the size of the array

   cout << "This program computes the average of 10 numbers" ;
   cout << endl ;

   


{

      ReadInput (A, number, i);

      ProcessData (A, number, i);

   } 

ComputeSum (sum, A);

ComputeMean (mean, sum);

PrintData (sum, mean);

return 0;
}

//Input Module (A)
void ReadInput (int A[10], int number, int i)
{
   for ( i = 0; i < MAX; i++) //create for loop
   {
	  cout << "Enter a positive number:" ;
      cin >> number ;
   }
}


void ProcessData (int A[MAX], int number, int i)
{
   if (number <= 0) // if else statement (INSIDE the loop) to calculate for user error
   {
         cout << "Invalid Entry! Enter a number greater than zero." ;
   }
      else if (number > 0)
	  {
         A[i] = number ;
   cout << endl;
   }
}


// Calculation Module (sum, mean)

double ComputeSum (int sum, int A[])
{
for (int i = 0; i < MAX; i++)

sum = sum + A[i] ;

return sum;

}

void ComputeMean (double mean, int sum)
{
{
   mean = 1.0 * sum / MAX;
}
cout << endl ;
}

// Output Module (sum, mean)

void PrintData (int sum, double mean)
{
cout << "The sum is" << sum << endl ;
cout << "The average is" << mean << endl ;
}

He/she means that you should add cout statements at different places to help narrow down where the problem is. Figure out what the variables' values SHOULD BE at certain times, add some cout statements and make sure that what they SHOULD BE is what they are. Here is your code:

/*******************************************************/
/*  File: Final Project                                */
/*                                                     */
/* Program to compute the average of ten integers      */
/*                                                     */
/* Inputs: (keyboard)                                  */
/* 1. ten numbers - negative numbers not allowed       */
/*                                                     */
/* Output:                                             */
/* Display the sum and average of the numbers input    */
/* using a constant for the array size                 */
/* and computing the average of all the elements       */
/* in the array                                        */
/*******************************************************/


#include <iostream>

using namespace std;

const int MAX = 10;

// Function prototypes, i.e., declarations for the functions we have created

void ReadInput (int A[], int number, int i);

void ProcessData (int A[MAX], int number, int i);

double ComputeSum (int sum, int A[]);

void ComputeMean (double mean, int sum);

void PrintData (int sum, double mean);

double mean (int sum, int i) ;
// compute an average, given a sum and the amount of numbers

// **************************************************************************



//Main Module

int main()

{
    // variable definitions


    int sum ; // declare sum as integer
    double mean; // declare mean as a double
    int i ; // declare i as integer
    int number; //declare number as integer

    // initialize variables


    i = 0;
    number = 0;
    sum = 0 ;
    mean = 0;
    int A[MAX] ; // declare the size of the array

    cout << "This program computes the average of 10 numbers" ;
    cout << endl ;

    {

        ReadInput (A, number, i);

        ProcessData (A, number, i);

    } 

    ComputeSum (sum, A);
    cout << "In main: Sum = " << sum << endl; // debugging statement
    ComputeMean (mean, sum);
    cout << "In main: Mean = " << mean << endl; // debugging statement

    PrintData (sum, mean);

    return 0;
}

//Input Module (A)
void ReadInput (int A[10], int number, int i)
{
    for ( i = 0; i < MAX; i++) //create for loop
    {
        cout << "Enter a positive number:" ;
        cin >> number ;
    }
}


void ProcessData (int A[MAX], int number, int i)
{
    if (number <= 0) // if else statement (INSIDE the loop) to calculate for user error
    {
        cout << "Invalid Entry! Enter a number greater than zero." ;
    }
    else if (number > 0)
    {
        A[i] = number ;
        cout << endl;
    }
}


// Calculation Module (sum, mean)

double ComputeSum (int sum, int A[])
{
    for (int i = 0; i < MAX; i++)
        sum = sum + A[i] ;

    cout << "In ComputeSum: sum = " << sum << endl;  // debugging statement
    return sum;
}

void ComputeMean (double mean, int sum)
{
    {
        mean = 1.0 * sum / MAX;
    }

    cout << "In ComputeMean: mean = " << mean << endl;  // debugging statement
    cout << endl ;
}

// Output Module (sum, mean)

void PrintData (int sum, double mean)
{
    cout << "The sum is" << sum << endl ;
    cout << "The average is" << mean << endl ;
}

Note lines 76, 78, 117, 127. I've added those lines. Remember to delete them later! These lines will help you narrow down where the problem is. They need to to calculate the correct values in the functions and the correct valu8es need to be stored in the variables in main AFTER the function is done. These cout statements will help you see if they are and help you narrow down where to start looking for the problems.

the code of post 14 compiles i think

First of all what is this supposed to be:

cout << "This program computes the average of 10 numbers" ;
   cout << endl ;

   


{

      ReadInput (A, number, i);

      ProcessData (A, number, i);

   } 

ComputeSum (sum, A);

For sure u either forgot sth or simply got confused (I mean {..} )

Start by doing what VernonDozier told u.

Hi again :D
First of all, I think u need to learn what references are and when u should use them.

Then u should decide whether u want your functions to be declared so that are void or if they should return sth and then use the same style for all of them.
for example u could use either of the following styles:

//declaration style 1:
void ComputeSum (int& sum, int A[])  //output a variable using references
{
sum=...
}
//usage style 1 e.g. in main:
computeSum(anArray);

//---------OR

//declaration style 2:
int ComputeSum (int [A]) 
{
...
int somesum=...  //locally declared
return somesum;
}
//usage style 2 e.g. in main:
sum=ComputeSum (anArray);

I cannot see the reason why u keep declaring and initializing the variables separately....

And finally here the code corrected:

#include <iostream>

const int MAX = 10;
bool RestartInputRead = false; //If a negative value is entered restart the number counting.

void ReadInput (int& number);
void ProcessData (int A[], const int& number, const int& i);
void ComputeSum (int& sum, const int A[]);
void ComputeMean (double& mean, const int& sum);
void PrintData (const int& sum,const  double& mean);

int main()
{
	int sum = 0;	// declare sum as integer + initialize here 
	//!!!!!!!!WHY DO U KEEP DOING IT SEPERATELY??
	double mean= 0; // declare mean as a double + initialize here
	int number = 0; //declare number as integer + initialize here
	int A[MAX] ;	//Declare an array of integers with size equal to MAX
	
	std::cout << "This program computes the sum & the average of 10 numbers"<<std::endl;	
	for (int i = 0; i < MAX; ++i) 
	{	
		ReadInput (number);
		ProcessData (A, number, i);
		while (RestartInputRead==true)//read & process input again, for each time a negative value was entered
		{
			ReadInput (number);
			RestartInputRead=false;
			ProcessData (A, number, i);
		}
	}
	ComputeSum (sum, A);
	ComputeMean (mean, sum);
	PrintData (sum, mean);
	return 0;
}
//read input from user
void ReadInput (int& number)
{  
	std::cout << "Enter a positive number:";
	std::cin >> number ;
}
//process vadility of user input and if valid save to array
void ProcessData (int A[], const int& number, const int& i)
{
	if (number <= 0) 
	{
		std::cout << "A negative entry is invalid !."<<std::endl ;
		RestartInputRead = true;
	}
	else if (number > 0)
	{
		A[i] = number ;
	}
}
// compute sum
void ComputeSum (int& sum, const int A[])
{
	for (int i = 0; i < MAX; ++i)	
		sum += A[i] ;
}
//Compute mean
void ComputeMean (double& mean, const int& sum)
{
		mean = 1.0 * sum / MAX;
}
//Give out results
void PrintData (const int& sum, const double& mean)
{
	std::cout << "The sum is equal to: " << sum << std::endl ;
	std::cout << "The average is equal to:  " << mean << std::endl ;
}

When u run it give on purpose negative values for lets say 5 times of the total 10, then the program will make sure to ask u for a valid input 10 times, not less not more. (see lines: 4 and 25 to 30)

There are neither errors nor warnings.
I hope the thread is finally closed.... :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.