Re write this code without functions?

Dec 11, 2009 at 2:20pm
Can someone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

void fill_array ( int golfArray[], int size);
double compute_average ( int golfArray[], int size);

int main()
{
	int scores[5];

	fill_array (scores, 5);
	cout << "The average is: " << compute_average(scores, 5) << endl;

	return 0;
}

void fill_array ( int golfArray[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << "Enter score for golfer number " << i+1 << ": ";
		cin >> golfArray[i];
	}
}

double compute_average ( int golfArray[], int size)
{
	double total=0;

	for (int i = 0; i < size; i++)
		total = total + golfArray[i];

	return total/size;
}
Dec 11, 2009 at 3:40pm
Replace the function calls with the code of the functions and replace the function parameters with the actual arguments.
Why do you need to do this anyway?
Dec 11, 2009 at 4:14pm
I want to fully understand the differences between functions and arrays and it's a little hard doing so with a program that uses both in one.

I will try to do this and reply with what I did
Dec 11, 2009 at 4:22pm
Dec 15, 2009 at 5:17am
I' still confused.

"Replace the function calls with the code of the functions and replace the function parameters with the actual arguments."

I'm not sure what does means. Can someone give me an example of one, and I will try and do the others?
Dec 15, 2009 at 5:48am
1
2
3
4
5
6
7
8
9
10
11
12
13
void printHello(string nameArg) { 

   cout<<"Hello "<<nameArg<<endl; 
}

int main() { 

  string name = "abc"; 
  printHello(name) ; 

return 0;
}


should be replaced with:

1
2
3
4
5
6
7
8
9

int main() { 

    string name="abc";
    cout<<"Hello "<<name<<endl; 

    return 0; 
}
Dec 15, 2009 at 5:49am
Fill array is done here. See if you can do the other function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

double compute_average ( int golfArray[], int size);

int main()
{
	const int size = 5;
	int scores[size];

	// fill_array
	for (int i = 0; i < size; i++)
	{
		cout << "Enter score for golfer number " << i+1 << ": ";
		cin >> scores[i];
	}

	cout << "The average is: " << compute_average(scores, 5) << endl;

	return 0;
}

double compute_average ( int golfArray[], int size)
{
	double total=0;

	for (int i = 0; i < size; i++)
		total = total + golfArray[i];

	return total/size;
}
Dec 15, 2009 at 6:38am
Just a few questions before I try it.

at the top the double compute_average ( int golfArray[], int size); Is this the function prototype? If I want this program purely all arrays would I need that? If I am trying to write this program without functions, do I simply delete that line? I know the double is what its returning, the next is function name, then to arguments it passes through, however is it safe to delete this line of code?

And I'm not sure what this line of code does?
compute_average(scores, 5)


And for the compute_average when writing it without a function, there are no cins right? Just a cout >> average ?

Last edited on Dec 15, 2009 at 6:39am
Dec 15, 2009 at 7:05am
at the top the double compute_average ( int golfArray[], int size); Is this the function prototype? If I want this program purely all arrays would I need that? If I am trying to write this program without functions, do I simply delete that line?


You don't need this, you can delete it and the function itself.

And I'm not sure what this line of code does?


That line of code is calling the function compute_average. You want to remove that as well and replace it will something like.

1
2
3
4
double total = 0;
/* add all the elements from scores to total */
total / size;
cout << total << endl;


And for the compute_average when writing it without a function, there are no cins right? Just a cout >> average ?


cout << average etc. Use << for output. >> is for input.
Topic archived. No new replies allowed.