|
查看: 1082|回复: 4
|
C Language : Call by Reference Vs Call by Value [已解决]
[复制链接]
|
|
|
- /*
- Homework for Chapter 5:
- Write a C program that accept a student quiz mark qm (per qt),
- before calculating the actual contribution (in %) of the quiz, qcp,to his or her total mark (per 100).
- e.g. Ali's obtained 45/60 (qm = 45, qt = 60) for his quiz.
- Since the quiz contributes to 15% (qcp= 15) towards the total mark,
- his current total mark is:?? qm*(qcp/qt) = 45 x (15/60) = 11.25
- The above formula must be written as a separate C function.
- Prepare two version of the program: use "call by value" for the 1st version, and "call by reference" for the 2nd version.
- */
- #include<stdio.h>
- float calculateMark(float fullQuiz,float quizMark,float conMark)
- {
- float totalMark=(quizMark/fullQuiz)*conMark;
- return totalMark;
- }
- ////////////////////////////////////////////////
- //////////1st Version: Call By Value////////////
- ////////////////////////////////////////////////
- void main()
- {
- float qm,qt,qcp,totalMark;
- char percent[]="%";
- printf("lease enter the full quiz mark: ";
- scanf("%f",&qt);
- printf("lease enter your quiz mark: ";
- scanf("%f",&qm);
- printf("The quiz contributes to how many mark towards the total mark? ";
- scanf("%f",&qcp);
- totalMark=calculateMark(qt,qm,qcp);
- printf("Your actual contribution (in %s) of the quiz to your total mark is %.2f%s.\n",percent,totalMark,percent);
- }
复制代码 ____________________________________________________________________________________________________________- /*
- Homework for Chapter 5:
- Write a C program that accept a student quiz mark qm (per qt),
- before calculating the actual contribution (in %) of the quiz, qcp,to his or her total mark (per 100).
- e.g. Ali's obtained 45/60 (qm = 45, qt = 60) for his quiz.
- Since the quiz contributes to 15% (qcp= 15) towards the total mark,
- his current total mark is:?? qm*(qcp/qt) = 45 x (15/60) = 11.25
- The above formula must be written as a separate C function.
- Prepare two version of the program: use "call by value" for the 1st version, and "call by reference" for the 2nd version.
- */
- #include<stdio.h>
- float calculateMark(float *pointer_qt,float *pointer_qm,float *pointer_qcp)
- {
- float quizMark,fullQuiz,conMark,totalMark;
- printf("lease enter the full quiz mark: ";
- scanf("%f",&fullQuiz);
- printf("lease enter your quiz mark: ";
- scanf("%f",&quizMark);
- printf("The quiz contributes to how many mark towards the total mark? ";
- scanf("%f",&conMark);
- *pointer_qt=fullQuiz;
- *pointer_qm=quizMark;
- *pointer_qcp=conMark;
- totalMark=(*pointer_qm / *pointer_qt) * *pointer_qcp;
- return totalMark;
- }
- ////////////////////////////////////////////////
- //////////2nd Version: Call By Reference////////
- ////////////////////////////////////////////////
- void main()
- {
- float qm,qt,qcp,totalMark;
- char percent[]="%";
- totalMark=calculateMark(&qt,&qm,&qcp);
- printf("Your actual contribution (in %s) of the quiz to your total mark is %.2f%s.\n",percent,totalMark,percent);
- }
复制代码 _____________________________________________________________________________________________________________
我不确定Call by reference的version是不是这样做,
总觉得怪怪的。
请大家指点迷津。。。
谢谢!~
[ 本帖最后由 蜡笔小烦 于 18-10-2008 03:29 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 6-10-2008 01:30 PM
|
显示全部楼层
給我的話我會這樣solve...
把fullQuiz, quizMark, conMark的address傳過去
其實很沒有意義...
把totalMark的address傳過去的話
不用return也可以改掉totalMark的value...
- #include<stdio.h>
- float calculateMark(float fullQuiz, float quizMark, float conMark, float *totalMark)
- {
- *totalMark=(quizMark / fullQuiz) * conMark;
- }
- ////////////////////////////////////////////////
- //////////2nd Version: Call By Reference////////
- ////////////////////////////////////////////////
- void main()
- {
- float quizMark, fullQuiz, conMark, totalMark;
- char percent[]="%";
- printf("lease enter the full quiz mark: ";
- scanf("%f",&fullQuiz);
- printf("lease enter your quiz mark: ";
- scanf("%f",&quizMark);
- printf("The quiz contributes to how many mark towards the total mark? ";
- scanf("%f",&conMark);
- calculateMark(fullQuiz, quizMark, conMark, &totalMark);
- printf("Your actual contribution (in %s) of the quiz to your total mark is %.2f%s.\n",percent,totalMark,percent);
- }
复制代码
[ 本帖最后由 cheng1986 于 6-10-2008 01:31 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-10-2008 01:01 AM
|
显示全部楼层
原帖由 cheng1986 于 6-10-2008 01:30 PM 发表 
給我的話我會這樣solve...
把fullQuiz, quizMark, conMark的address傳過去
其實很沒有意義...
把totalMark的address傳過去的話
不用return也可以改掉totalMark的value...
#include
float calculateMark(fl ...
对~
我就是觉得我那做法很多余~
还学不精的说
还请多多指教!~
谢谢你~
ps:有个warning说 'calculateMark' : must return a value
不用管它吗? |
|
|
|
|
|
|
|
|
|
|
发表于 7-10-2008 01:31 AM
|
显示全部楼层
|
function type要改成void..... |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-10-2008 02:00 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|