Arrays and functions calling

i wrote a code to read a number of grades from a file and display(cout

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1137 Views - Last Post: 12 March 2009 - 05:51 PM Rate Topic: -----

#1 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Arrays and functions calling

Posted 12 March 2009 - 03:10 PM

My code is running and compiling ok, but i would like to have it in a special order. i would like to have the functions before main and than use just the calling in main(). I oculdn't figure out the function for the average and how to call it, everytime i try it gives me some weird number.



//all programs start here in main function
int CalculateHighest(int x, int y){	   //function to calculate the Maximum
	if (x>=y){return x;} 
	else {return y;}
}
int CalculateLowest(int a, int b){
	if (a<=b){return a;}
	else {return b;}
	}

int main(){

	ifstream in("num.txt");
	int array[101], data_size = 0;
	float sum=0;
	float average;int Max;int Min;
	if(in.is_open()==false)
		 cout<<"Hey couldnt open txt file!!!!"<<endl;
	else{
		while(in.peek()!=EOF){ 
			in>>array[data_size];			   //Caution here!! If indx is greater than 5, you will over index the array
			data_size++;
		}
	}	
	//Print out all stuff in array
	for(int j = 0; j<data_size; j++){		   // to get the sum used for the average 
			  sum= sum+array[j];	  
							   }
	cout.setf(ios::fixed); // Shows Numbers in Normal notation. 
	cout.setf(ios::showpoint); // This shows the decimal point.
	cout.precision(2); // This rounds the output to 2 digits past the decimal.
	average=sum/data_size;
	cout<<"Number of grades is "<<data_size<<endl;	//number of grades in the file or array
	cout<<"Average grade is "<<average<<endl;	//printout the average of the grades
	
	 for(int i=0;i<data_size-1;i++){							   //function for the maximum
			Max=CalculateHighest(array[i],array[i+1]);}
		   cout<<"Maximum grade is "<<Max<<endl;
	
	 for(int i=0;i<data_size-1;i++){			
			Min=CalculateLowest(array[i],array[i++]);}	  
		   cout<<"Minimum grade is "<<Min<<endl;
		   
	 int above=0;int below=0;							   // to calculate the number of grades above/below the average
															// using a for loop.
	 for(int i=0; i<data_size; i++){
			 if (array[i]>average){								   
								   above++;}
								  
			 else if (array[i]<average){
									below++;
								   }
								   }
	  cout<<"The number of grades above the average is: "<<above<<"\n";	   
	 cout<<"The number of grades below the average is: "<<below<<endl;
	in.close();
	system("pause");				//causes the system to pause when executing, to view print outs
	//not needed in Visual C++ 2008
	return 0;					   //return 0 to terminate the main function because its data type is int
}



What i am trying to do, is having five functions before the main and jsut call them in the main. It worked for calculating the highest and lowest, but not for the average.
Thank you


Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and functions calling

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Arrays and functions calling

Posted 12 March 2009 - 03:13 PM

You use prototypes.

Wait what? I totally mis understood what you're asking... You want to know how to call a function?

This post has been edited by Hyper: 12 March 2009 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#3 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 03:21 PM

View PostHyper, on 12 Mar, 2009 - 02:13 PM, said:

You use prototypes.

Wait what? I totally mis understood what you're asking... You want to know how to call a function?


i know how to call a function, but i am not getting to write a function for the average of a sequence of numbers stored in an array. And in main(), i would like to call the funtion.
Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Arrays and functions calling

Posted 12 March 2009 - 03:33 PM

View Postmoe_3_moe, on 12 Mar, 2009 - 02:21 PM, said:

but i am not getting to write a function for the average of a sequence of numbers stored in an array.


Your instructor (teacher?) said you cannot write a function and you want to?
Was This Post Helpful? 0
  • +
  • -

#5 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 03:42 PM

View PostHyper, on 12 Mar, 2009 - 02:33 PM, said:

View Postmoe_3_moe, on 12 Mar, 2009 - 02:21 PM, said:

but i am not getting to write a function for the average of a sequence of numbers stored in an array.


Your instructor (teacher?) said you cannot write a function and you want to?


No, he said it is ok like that since it is doing the right thing, but i am a beginner and i am willing to learn how to be efficient in writing codes. I know that it is always good to define the function and only call them in the main, which is the way we will be using when we start structures next week.
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Arrays and functions calling

Posted 12 March 2009 - 03:51 PM

Create a function -> pass the array -> calculate the average
Done.
Was This Post Helpful? 0
  • +
  • -

#7 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 03:59 PM

View PostHyper, on 12 Mar, 2009 - 02:51 PM, said:

Create a function -> pass the array -> calculate the average
Done.


I did, and when i called it i passed the array and the indx as variables, but did work. here it is .

#include <iostream>						//standard C++ IO operations using streams
#include <cstdlib>						 //standard General Purpose functions (atoi, memalloc, ...)
#include <math.h>
#include <fstream>
#include <string>

using namespace std;

//all programs start here in main function
int CalculateHighest(int x, int y){	   //function to calculate the Maximum
	if (x>=y){return x;} 
	else {return y;}
}
int CalculateLowest(int a, int b){
	if (a<=b){return a;}
	else {return b;}
	}
double average(int a, int b){
	   double sum=0;
	   for (int i=0; i<b;i++){
		   sum=sum+a;
		   }
		   return sum/b;
		   }

int main(){

	ifstream in("num.txt");
	int array[101], data_size = 0;
	double sum=0;
	float average;int Max;int Min;
	if(in.is_open()==false)
		 cout<<"Hey couldnt open txt file!!!!"<<endl;
	else{
		while(in.peek()!=EOF){ 
			in>>array[data_size];			   //Caution here!! If indx is greater than 5, you will over index the array
			data_size++;
		}
	}	
	//Print out all stuff in array
	for(int j = 0; j<data_size; j++){		   // to get the sum used for the average 
			  sum= sum+array[j];	  
							   }
	cout.setf(ios::fixed); // Shows Numbers in Normal notation. 
	cout.setf(ios::showpoint); // This shows the decimal point.
	cout.precision(2); // This rounds the output to 2 digits past the decimal.
	double ave;
	ave=average(array[data_size],data_size);
	cout<<"Number of grades is "<<data_size<<endl;	//number of grades in the file or array
	cout<<"Average grade is "<<ave<<endl;	//printout the average of the grades
	
	 for(int i=0;i<data_size-1;i++){							   //function for the maximum
			Max=CalculateHighest(array[i],array[i+1]);}
		   cout<<"Maximum grade is "<<Max<<endl;
	
	 for(int i=0;i<data_size-1;i++){			
			Min=CalculateLowest(array[i],array[i++]);}	  
		   cout<<"Minimum grade is "<<Min<<endl;
		   
	 int above=0;int below=0;							   // to calculate the number of grades above/below the average
															// using a for loop.
	 for(int i=0; i<data_size; i++){
			 if (array[i]>average){								   
								   above++;}
								  
			 else if (array[i]<average){
									below++;
								   }
								   }
	  cout<<"The number of grades above the average is: "<<above<<"\n";	   
	 cout<<"The number of grades below the average is: "<<below<<endl;
	in.close();
	system("pause");				//causes the system to pause when executing, to view print outs
	//not needed in Visual C++ 2008
	return 0;					   //return 0 to terminate the main function because its data type is int
}


Was This Post Helpful? 0
  • +
  • -

#8 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Arrays and functions calling

Posted 12 March 2009 - 04:00 PM

View Postmoe_3_moe, on 12 Mar, 2009 - 02:59 PM, said:

I did, and when i called it i passed the array and the indx as variables, but did work. here it is .


Congrats then.

EDIT: When I tried to compile it (with Dev-C++) I got this:
48 asfasfas.cpp `average' cannot be used as a function

This post has been edited by Hyper: 12 March 2009 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 04:06 PM

View PostHyper, on 12 Mar, 2009 - 03:00 PM, said:

View Postmoe_3_moe, on 12 Mar, 2009 - 02:59 PM, said:

I did, and when i called it i passed the array and the indx as variables, but did work. here it is .


Congrats then.

EDIT: When I tried to compile it (with Dev-C++) I got this:
48 asfasfas.cpp `average' cannot be used as a function


SO.... lol .... i can't tell where is the problem?
Was This Post Helpful? 0
  • +
  • -

#10 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: Arrays and functions calling

Posted 12 March 2009 - 04:14 PM

I understand your still learning, but your for loops are everywhere, and they kind of confuse me, haha

Either way, I'll show you how to create a proper function to pass arrays and calculate the average, since I remember when I started
So, I'll edit your function that you have given.

int b, is obviously the size of the array, we will need this, so that is good, but instead of calling it just 'b'
I'm calling it something meaningful, like "size"
//double average(int a, int b)  //old function
double average(int arrayGiven[], int size)  //new function
{
	   double sum=0;  //this is fine
/*	   for (int i=0; i<size;i++)
	   {
		   sum=sum+a;	  /////////////old function
	   }  */


//new for loop
for(int i = 0; i<size; i++)
{
sum = sum + arrayGiven[i];  //goes through each element of the array, passed and adds it all up
}

		   return sum/size;  //just make note, size is typecasted from being an  int, to a double
}


Please, if you have questions about what I did, just ask.
Was This Post Helpful? 0
  • +
  • -

#11 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 04:29 PM

Thank you for your help, but still giving same error, i am afraid i might be calling the function wrong?
Was This Post Helpful? 0
  • +
  • -

#12 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Arrays and functions calling

Posted 12 March 2009 - 04:31 PM

"average" is being used by something else. Just rename it to ABC or something.
Was This Post Helpful? 0
  • +
  • -

#13 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 04:44 PM

View PostHyper, on 12 Mar, 2009 - 03:31 PM, said:

"average" is being used by something else. Just rename it to ABC or something.


Oh yeahhhhhhh, you are right.... but now i have a new problem.
" invalid conversion from `int' to `int*' "
" initializing argument 1 of `double ave_rage(int*, int)' "
??
Was This Post Helpful? 0
  • +
  • -

#14 Bot-Vector  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 7
  • Joined: 08-January 09

Re: Arrays and functions calling

Posted 12 March 2009 - 05:10 PM

Before your first function:

void ave_rage(int[], int);



then in your actual function:

void ave_rage(int[] a, int b)
{
  '// rest of function here
}


Make sure you're only passing the name of the array into the function
Was This Post Helpful? 1
  • +
  • -

#15 moe_3_moe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 12-March 09

Re: Arrays and functions calling

Posted 12 March 2009 - 05:21 PM

"
Make sure you're only passing the name of the array into the function"

i was calling the array in the function by its name and size, that was my mistake.

Thank you.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2