Click here to Skip to main content
15,393,706 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include "stdafx.h"
#include <iostream>
using namespace std;
void f(int&, int);
int main()
{
 int N, s;
 cout << "how many? ";
 cin >> N;
 f(N, s);
 cout << "The result = " << (double) s / N << endl;
 return 0;
}
void f(int &n, int s)
{
 int m;
 cout << "enter " << n << " integers:- " << endl;
 for (; n > 0; n--) {
cin >> m;
s = s + m;
 }
}


What I have tried:

Read through the program listed below carefully. The program contains errors.
(i) Correctly identify the errors, describe them and explain their causes in the answer book.
Be aware that identifying errors incorrectly will result in reduction of your marks.
(15 marks)
(ii) Make minimal amount of changes to the program to correct these errors. Save the
corrected program into the designated folder for this question.
(10 marks)
Posted
Updated 7-Jun-17 21:26pm
v3
Comments
Afzaal Ahmad Zeeshan 8-Jun-17 3:23am
   
You are meant to do your homework yourself. Sorry.
CPallini 8-Jun-17 3:29am
   
Are you sure it is an exam question? The program is rather ugly...

1 solution

Hi Member 13246826,

As this is your homework, you need to think about it. I see the problem but I should not be telling you the solution.

However, I can give you a hint - read about passing parameter by value, and passing parameter by reference. I hope you will find the answer and correct the code as needed.
   
Comments
Member 13246826 7-Jun-17 19:55pm
   
Hello Mehedi Shams,

Thank you for the Hint, Its not a homework, its a past exam question for PSP1, c++ .

As my exam is tomorrow so thats why i was trying to solve them.
Mehedi Shams 7-Jun-17 20:02pm
   
Ok got it.

But still - please follow the hint. It is so trivial and can't be missed. Hope you will find the answer :)!

Just as another hint - the actual value of 'N' is decreasing. See why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900