This is an urgent! help needed...

1. Create a program that will ask for 5 values for an array of integer then compute and display the sum and average of the elements. The program will use getSumAverage() to ask for the values, compute and display the sum of the elements.
2. Revise exercise 1, the getSumAverage() will be used to compute and display the sum and average and will use the array as its formal parameter.
3. Revise exercise 2, the getSumAverage() will be used to compute and display the sum then return the value of the average.

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
(for number 1)
#include<iostream.h>
#include<conio.h>
int getSumAverage(int a[5]);
int b;
main()
{
	clrscr();
	int x[5], y;
	cout<<"Enter 5 values: "<<endl;
		for(y=0; y<5; y++)
		{
			cin>>x[5];
		}
	cout<<"The sum is "<<getSumAverage(x);
getch();
return 0;
}
	int getSumAverage(int a[5]){
	for (int i=0; i<5; i++)
	b=b+a[i];
	return b;
	}


what must be the correct way to solve this problems together with the other 2..
I'm so sorry i am just a beginner but I really dont know anything about functions and that our teacher gave this as our project even though he did not teach us this...

If you could also solve this:
Create a program that will ask for 5 values of an array of integer
that represents 5 items’ prices bought by the customer. Compute and
display the total amount to pay. Ask for the cash then compute and display
the change. The program will use computeTotal() to compute and return the
total amount to pay and will use the array as its parameter. And computeChange()
to compute and return the change and will use the total amount and the cash
as its parameter.

thnks again... really need this by tomorrow..!
Last edited on
If you could also solve this:
Create a program that will ask for 5 values of an array of integer
that represents 5 items’ prices bought by the customer. Compute and
display the total amount to pay. Ask for the cash then compute and display
the change. The program will use computeTotal() to compute and return the
total amount to pay and will use the array as its parameter. And computeChange()
to compute and return the change and will use the total amount and the cash
as its parameter.


I hope that's what you asked for!


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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>

using namespace std;

int computeTotal(int arr[5])
{
    int total = 0;
    for(int i = 0; i < 5; i++)
    {
        total += arr[i]; //equivalent to total = total + arr[i]. incase you didn't know.
    }

    return total;
}

int computeChange(int total, int cash)
{
    return abs(total - cash); //so we don't get a -ve answer.
}

int main()
{
    int items[5];
    int cash;

    cout << "Enter the items values: " << endl;
    for(int i = 0; i < 5; i++)
    {
        cout << "$";
        cin >> items[i];
    }
    int total = computeTotal(items);
    cout << "The Total of your Items is: $" << total << endl;

    cout << "Please enter the amount you'll pay: $" ;
    cin >> cash;
    while(cash < total)
    {
        cout << "You cannot pay less than the total price, try again: $";
        cin >> cash;
    }

    int change = computeChange(total,cash);

    cout << "Your change is: $" << change;

    return 0;
}
Also, about the getSumAverage question you mentioned first and asked for help with, you had a few mistakes with your code, some on-topic and some off-topic

First:[Off-Topic]
The use of cout << ... without
1
2
3
using std::cout; //using std::cin; is the same
//or 
using namespace std; //for general use 

Second:
1
2
3
4
for(y=0; y<5; y++)
{
cin>>x[5];
}


Here, you're basically inputting to x[5] which is out of the array boundaries if you have an x[5] , the elements indexes are from 0 - 4 which is 0 to n-1 (5-1). so you want to input to x[y] instead of that so you go from 0 to 4 while inputting elements.

Third: int getSumAverage(int a[5])
In this specific function the use of int is kind of un-necessary cause you want to return 2 values (avg & sum) which you can't do that unless you use structs, so you want a void type in order to print them within the function.


Here is a simple fix for your code:

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
#include<iostream>

using namespace std;

void getSumAverage(int arr[5])
{
    int total = 0;
    for(int i = 0; i < 5; i++)
    {
        total += arr[i];
    }
    cout << "The total of your array's elements is: " << total << endl;
    cout << "The average of your array's elements is: " << (total/5) << endl; //sum divided no.elements
}



int main()
{

    int x[5], y;
    cout<<"Enter 5 values: "<<endl;
    for(y=0; y<5; y++)
    {
        cin >> x[y];
    }
    getSumAverage(x);

    return 0;
}

Last edited on
Yay!!! Thank you Bliink i really appreciate the help!! May you have more blessings in life!!!
Yay!!! Thank you Bliink i really appreciate the help!! May you have more blessings in life!!!


Np buddy
Topic archived. No new replies allowed.