I have to write five function which i do not have to put in "main" but have to call inside main. i hade come up with a code which did not work and was incomplete but i was futher told i have to use global variables and not local variable like i had. Bythe way i am a nowbie so please try to make clear comments so i can see what is been done.

I have to (1) make a function to input an array of numbers(the number of elements not specified) with the length and elements as variables.
(2) function to add all the elements
(3) function to display original array and sum
(4) function to find average and display
(5) functon to find square of individual elements and sum of array
my code was as follows:

using namespace std;
int length; // length of array
int elements; //elements of the array
int t; // counter
int y[]; // array
int sum=0;
void input(length,elements)
   cout <<" How many alements is the array to have\n";
          cout<<"Enter elements\n";
void sum(length,y[],t,sum)
    sum = 0;
         sum += y[t]

 cout<< sum;

void display(t,y[],length);
      cout<< y[t]\n

i hope you will make the solution easy enough for me to understand

9 Years
Discussion Span
Last Post by WaltP

your program is missing a closing parentheses at line 18 for funciton input()

line 6: that is normally coded with the asterisk, like this: int *y; .

lines 18 and 30. you have to specify the data types as well as variable names

void sum(int length,int y[], int t, int sum)

I chnged the code as follows but it doesnt sim to stop compiling

1. #include<iostream>
2. using namespace std;
3. void input(int *y,double elements)
4. {
5.          cout<<"Enter elements\n";
6.         *y;
7.		  return;        
8. }
9. void add(int *y);
10. {
11.   double sum = 0;
12.  sum += *y;
13.   cout<< sum;
14.  return;
15. }          
16. void display(int *y);
17. {
18.      cout<< *y;
19.   return;
20. }

Line 9 and 16 of the code in post #3 are the first lines of function definitions and should not be terminated with a semicolon. Function declarations are terminated by a semicolon, but they aren't followed by a function body (the stuff between the {}s after the first line of the function definition.


i hope i am not asking too much now
the change has been make and now i have to call the functions in "main". I have been going through trial and error to fing the correct way of doing this.

the code presented before "main" has no errors, with me putting in main as shown; an arror appears saying "input does not take in 0 parameters"

How am a i supposed to call the function in such a way that it will then ask the user to input the array?

P.S is there an easeir way to include the numbers of the lines or do i have to type them myself all the time?

2.using namespace std;
3.void input(int *y,double elements)
5.      cout<<"Enter elements\n";
6.	  cin>>  elements;
7.        *y;
8.		  return;        
10. void add(int *y)
11. {
12.   double sum = 0;
13.  sum += *y;
14.   cout<< sum;
15.  return;
16. }          
17.void display(int *y)
19.      cout<< *y;
20.   return;
23.int main()
25.	  input ();

Your function declaration for input says it should receive two parameters, a pointer to an int and a double. When you call the function you need to give it that information.

Also, just because the code compiles without syntax errors doesn't mean it's going to give the output you expect because run time errors and logic errors can still be present.

2. using namespace std;
3. void input(double *y,double elements)
4. {
5.      for (int t =0; t<elements; t++)
6.	  {
7.		  cout <<" Leasse enter the array["<<t++<<"]";
8.		  cin >> y[t];
9.	  }
10. }
11. void display(double *y, int elements)
12. {
13.	for (int t = 0;t<elements; t++)
14.	 {
15.		cout<< y[t];
16.	 }   
17. }
18. double add(double *y, int elements)
19. {
20.   double sum = 0;
21.   for (int t = 0; t < elements;t++)
22.   {
23.       sum += y[t];
24.   }
25.   return sum;
26. }
27. double mean(double *y, int elements)
28. {
29.	 if (elements > 0)
30.		 return add(y, elements) / elements;
31.	 else
32.		 return(0);
33. }
34. double * square(double *y,int elements)
35. {
36.	 double *Tempy = new double[elements];
37.	 for (int t=0;t<elements;t++)
38.	 {
39.		 Tempy[t]=y[t] * y[t];
40.	 }
41.	 return Tempy;
42. }
43. int main()
44. {
45.	 double *y;
46.    int elements;
47.	 cout << "Please enter the number of elements that u plan to have in the array   \n";
48.	 cin >> elements;
49.	 y = new double[elements];
50.	 input(y,elements);
51.	 cout<<"The array you have entered is \n";
52.	 display(y,elements);
53.	 add(y,elements);
54.	 cout<<"That is the sum of the elements of your array\n";
55.	 mean(y,elements);
56.	 cout<<"That is the mean of all the elements you have entered as you array\n";
57.	 return (0);
59. }

I have made a few changes but it really still does not do wat i was xpecting


I have made a few changes but it really still does not do wat i was xpecting

That's too bad. Unfortunately most of us aren't going to play with the program to try to figure out what it's not doing. You need to tell us what it's doing wrong. This isn't www.psychics.com you know...


ok, fair enough. the program runs properly but my two last functions dont seem to be executed, namely "add" and " average"
my output is as shown below

Please enter the number of elements that u plan to have in the array
Please enter the array[0]1
Please enter the array[1]2
Please enter the array[2]3
Please enter the array[3]4
Please enter the array[4]5
The array you have entered is
1, 2, 3, 4, 5]
The sum of the elements in your array is
That is the mean of all the elements you have entered as you array
Press any key to continue

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.