Rocky2013 Posted October 27, 2013 Share Posted October 27, 2013 a) Write a function, named getNumSum(), for accumulating(summing)float numbers: ->The function has no return value ->The function has 1 input argument named aSum: the current accumulative sum to be "updated" inside this function(Hint: use pass by reference) ->The function asks user for a float input, then updates the accumulative sum ->No code for input validation needed Link to comment Share on other sites More sharing options...
Sensei Posted October 27, 2013 Share Posted October 27, 2013 (edited) void getNumSum( float &aSum ) { float data = 0.0; // ask user for float here... aSum += data; } Edited October 27, 2013 by Sensei Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 27, 2013 Author Share Posted October 27, 2013 Thanks but it is not work You means #include<stdio.h> void getNumSum( float aSum ); int main() { float aSum; float data = 0.0; printf("Please input a float number: "); scanf("%f", &aSum); aSum += data; } Link to comment Share on other sites More sharing options...
Sensei Posted October 28, 2013 Share Posted October 28, 2013 (edited) Rather something like this: #include <stdio.h> void getNumSum( float &aSum ) // do you see difference here?? Parameter must be with & { float data = 0.0; printf("Please input a float number: "); scanf("%f", &data ); aSum += data; } void main() { float aSum = 0.0; for( int i = 0; i < 5; i++ ) { getNumSum( aSum ); printf( "Sum %f\n", aSum ); } } Edited October 28, 2013 by Sensei Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 28, 2013 Author Share Posted October 28, 2013 Error occurs when compiler Link to comment Share on other sites More sharing options...
Sensei Posted October 28, 2013 Share Posted October 28, 2013 (edited) Can't you simply copy and paste to compiler what I wrote in post #4? This time I have compiled above code, and tested and worked as intended... Edited October 28, 2013 by Sensei Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 28, 2013 Author Share Posted October 28, 2013 Show you my result Link to comment Share on other sites More sharing options...
Sensei Posted October 28, 2013 Share Posted October 28, 2013 It's not exactly what I posted in #4. Compare lines one by one.. Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 28, 2013 Author Share Posted October 28, 2013 The same Sorry to disturb you Link to comment Share on other sites More sharing options...
Sensei Posted October 28, 2013 Share Posted October 28, 2013 You named file .c but it's .cpp code Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 28, 2013 Author Share Posted October 28, 2013 Could you write it in .c Link to comment Share on other sites More sharing options...
Sensei Posted October 28, 2013 Share Posted October 28, 2013 (edited) References are in cpp, not in old c. Rename file and should be fine. Edited October 28, 2013 by Sensei Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 28, 2013 Author Share Posted October 28, 2013 Teacher ask me finishing this lab in c programme so could you teach me how to achieve the above target in c programme, Please Link to comment Share on other sites More sharing options...
Sensei Posted October 28, 2013 Share Posted October 28, 2013 Then your teacher is incompetent.. Because it's in flagrant conflict with this "(Hint: use pass by reference)"... In ANSI C, you have to replace references by pointers. #include <stdio.h>void getNumSum( float *aSum ){float data = 0.0;printf("Please input a float number: ");scanf("%f", &data );*aSum += data;}void main(){float aSum = 0.0;for( int i = 0; i < 5; i++ ){getNumSum( &aSum );printf( "Sum %f\n", aSum );}} Link to comment Share on other sites More sharing options...
Rocky2013 Posted October 30, 2013 Author Share Posted October 30, 2013 What is the difference between the above lab and the lab show you below. Link to comment Share on other sites More sharing options...
Sensei Posted October 30, 2013 Share Posted October 30, 2013 Mine code is for loop 5 times, not 3. And text displayed to user is different. fSum instead of aSum as name. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now