佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1083|回复: 4

C Language : Call by Reference Vs Call by Value [已解决]

[复制链接]
发表于 3-10-2008 09:56 PM | 显示全部楼层 |阅读模式
  1. /*
  2. Homework for Chapter 5:
  3. Write a C program that accept a student quiz mark qm (per qt),
  4. before calculating the actual contribution (in %) of the quiz, qcp,to his or her total mark (per 100).
  5. e.g.    Ali's obtained 45/60 (qm = 45, qt = 60) for his quiz.
  6.   Since the quiz contributes to 15% (qcp= 15) towards the total mark,
  7.   his current total mark is:??    qm*(qcp/qt) = 45 x (15/60) =  11.25
  8. The above formula must be written as a separate C function.
  9. Prepare two version of the program: use "call by value" for the 1st version, and "call by reference" for the 2nd version.
  10. */
  11. #include<stdio.h>
  12. float calculateMark(float fullQuiz,float quizMark,float conMark)
  13. {
  14. float totalMark=(quizMark/fullQuiz)*conMark;
  15. return totalMark;
  16. }
  17. ////////////////////////////////////////////////
  18. //////////1st Version: Call By Value////////////
  19. ////////////////////////////////////////////////
  20. void main()
  21. {
  22. float qm,qt,qcp,totalMark;
  23. char percent[]="%";
  24. printf("lease enter the full quiz mark: ";
  25. scanf("%f",&qt);
  26. printf("lease enter your quiz mark: ";
  27. scanf("%f",&qm);
  28. printf("The quiz contributes to how many mark towards the total mark? ";
  29. scanf("%f",&qcp);
  30. totalMark=calculateMark(qt,qm,qcp);
  31. printf("Your actual contribution (in %s) of the quiz to your total mark is %.2f%s.\n",percent,totalMark,percent);
  32. }
复制代码
____________________________________________________________________________________________________________
  1. /*
  2. Homework for Chapter 5:
  3. Write a C program that accept a student quiz mark qm (per qt),
  4. before calculating the actual contribution (in %) of the quiz, qcp,to his or her total mark (per 100).
  5. e.g.    Ali's obtained 45/60 (qm = 45, qt = 60) for his quiz.
  6.   Since the quiz contributes to 15% (qcp= 15) towards the total mark,
  7.   his current total mark is:??    qm*(qcp/qt) = 45 x (15/60) =  11.25
  8. The above formula must be written as a separate C function.
  9. Prepare two version of the program: use "call by value" for the 1st version, and "call by reference" for the 2nd version.
  10. */
  11. #include<stdio.h>
  12. float calculateMark(float *pointer_qt,float *pointer_qm,float *pointer_qcp)
  13. {
  14. float quizMark,fullQuiz,conMark,totalMark;
  15. printf("lease enter the full quiz mark: ";
  16. scanf("%f",&fullQuiz);
  17. printf("lease enter your quiz mark: ";
  18. scanf("%f",&quizMark);
  19. printf("The quiz contributes to how many mark towards the total mark? ";
  20. scanf("%f",&conMark);
  21. *pointer_qt=fullQuiz;
  22. *pointer_qm=quizMark;
  23. *pointer_qcp=conMark;
  24. totalMark=(*pointer_qm / *pointer_qt) * *pointer_qcp;
  25. return totalMark;
  26. }
  27. ////////////////////////////////////////////////
  28. //////////2nd Version: Call By Reference////////
  29. ////////////////////////////////////////////////
  30. void main()
  31. {
  32. float qm,qt,qcp,totalMark;
  33. char percent[]="%";
  34. totalMark=calculateMark(&qt,&qm,&qcp);
  35. printf("Your actual contribution (in %s) of the quiz to your total mark is %.2f%s.\n",percent,totalMark,percent);
  36. }
复制代码
_____________________________________________________________________________________________________________

我不确定Call by reference的version是不是这样做,
总觉得怪怪的。
请大家指点迷津。。。
谢谢!~

[ 本帖最后由 蜡笔小烦 于 18-10-2008 03:29 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 6-10-2008 01:30 PM | 显示全部楼层
給我的話我會這樣solve...
把fullQuiz, quizMark, conMark的address傳過去
其實很沒有意義...

把totalMark的address傳過去的話
不用return也可以改掉totalMark的value...

  1. #include<stdio.h>
  2. float calculateMark(float fullQuiz, float quizMark, float conMark, float *totalMark)
  3. {
  4. *totalMark=(quizMark / fullQuiz) * conMark;
  5. }
  6. ////////////////////////////////////////////////
  7. //////////2nd Version: Call By Reference////////
  8. ////////////////////////////////////////////////
  9. void main()
  10. {
  11. float quizMark, fullQuiz, conMark, totalMark;
  12. char percent[]="%";
  13. printf("lease enter the full quiz mark: ";
  14. scanf("%f",&fullQuiz);
  15. printf("lease enter your quiz mark: ";
  16. scanf("%f",&quizMark);
  17. printf("The quiz contributes to how many mark towards the total mark? ";
  18. scanf("%f",&conMark);
  19. calculateMark(fullQuiz, quizMark, conMark, &totalMark);
  20. printf("Your actual contribution (in %s) of the quiz to your total mark is %.2f%s.\n",percent,totalMark,percent);
  21. }
复制代码

[ 本帖最后由 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 | 显示全部楼层
原帖由 cheng1986 于 7-10-2008 01:31 AM 发表
function type要改成void.....


对啊~
没有察觉到
不好意思
谢谢!~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 23-12-2025 06:55 PM , Processed in 0.112494 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表