Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,209,226 members, 8,005,311 topics. Date: Sunday, 17 November 2024 at 08:18 PM |
Nairaland Forum / Science/Technology / Programming / Please Help Me Check Whats Wrong With My Code(c Language) (2154 Views)
Php Gurus Help Me Out: What Is Wrong With My Code?? / Please Help Me Look At My Code (2) (3) (4)
Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 1:36am On Apr 10, 2015 |
hi i started learning programming two weeks ago. being practicing coding since then. i wrote a program to calculate gpa, but my program don't execute well despite my compilers (c-free and dev c++) not dictating any bug. please take a look at my code and help me out. it does not include 'credit unit' while calculating. it's a c program: /*A c programe to calculate GPA*/ 1.#include<stdio.h> 2.int main() 3.{ 4. char grade;//declares the variable "grade" of type char 5. float gpa;//declares the variable "gpa" of type floating point 6. gpa=0;//initialize the value to zero 7. int sum;//declares the variable "sum" 0f type integer 8. sum=0;//initialize the value to zero 9. int n;//declares the variable "n" 0f type integer 10. printf("enter number of courses:\n"//prompts user to enter number of courses 11. scanf("%d",&n);//collects the number and stores it in th variable 'n' 12. int a[n];//declares an array of n-elements 13. int i;//declares a counter 14. i=0;//initialize the counter 15. do //commence a loop with do...while 16. { 17. int y;//declares a variable 'y' 18. y=a[i];//stores the array in the variable 'y' 19. printf("enter course%d grade:units \n",i+1);//prompts user to enter course grade and credit units 20. scanf("%c\t%d",&grade,&a[i]);//stores the grade in the variable 'c' and units in the array 'a' 21. switch(grade)//commence a switch...case mode to test and assign grades /*this multiplies the grade and unit for each entry and adds them up*/ 22. { 23. case 'A': 24. sum=sum+y*5; 25. break; 26. case 'B': 27. sum=sum+y*4; 28. break; 29. case 'C': 30. sum=sum+y*3; 31. break; 32. case 'D': 33. sum=sum+y*2; 34. break; 35. case 'E': 36. sum=sum+y*1; 37. break; 38. } 39. i++; 40. } 41. while (i<n);//loop terminates when all courses are entered 42. gpa=sum/n;//this calculates the GPA using the formula 'GPA=total score/total credit units 43. printf("total credit unit is %d\n total score is %d\n",n,sum);//this displays the total units and the total score 44. printf("GPA=%f\n",gpa);//this displays the GPA 45. return 0; 46.}/*program written by Patrick*/ 1 Like |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Kofacts(m): 2:12pm On Apr 10, 2015 |
What error does it show ? |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by aerodee(m): 2:37pm On Apr 10, 2015 |
Line 10, the smiley ; ) , it should be ) ; line 17, all initialization in C comes in the beginning of the main() you may want to put default in your switch for ppl that enter grade outside the range allowed. etc Abeg I don tire, I need water |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 2:54pm On Apr 10, 2015 |
Kofacts:it does'nt show any error; i hope these images will help u understand my problem
|
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 2:57pm On Apr 10, 2015 |
aerodee:tnx baba i get one tanker of water 1 Like |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by tjones007: 11:37pm On Apr 10, 2015 |
Why stuck the code with irrelevant comments. While comments are good and best thing to be in a code. It should be used explain what can be understood. Comments are good. Don't get me wrong. |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 11:56pm On Apr 10, 2015 |
tjones007:noted sir. I'm really a newbie in programming. just wanted u guys to understand my algorithm. |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Nobody: 11:28am On Apr 13, 2015 |
Pato5:No cout; or cin in 33 to 37 rewrite it again and u end the program with a comment instead of} You the comment at the heading should be like this 1. /* 2. * c program to calculate GPA (Gpa.cpp) 3 */ 22, 38, and 40 is invalid After #include <stdo> h // needed to perform IO operations Put using namespace std; // Also needed for <string> |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 1:33pm On Apr 13, 2015 |
proxy23:it's a c program not c++. i thought "cout,std" are c++ syntax |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by kudaisi(m): 7:14pm On Apr 13, 2015 |
This should solve the course unit not showing problem. Add a space before %c in scanf. This will allow scanf to skip any number of white spaces before reading choice so this 19. printf("enter course%d grade:units \n",i+1);//prompts user to enter course grade and credit unitswill become printf("enter grade for course%d \n",i+1);NOTE THE SPACES in scanf statements Another observation is that line 17 and 18 are totally unnecessary. Since you can simply use variable a[i] in the switch cases line 24 for example will be sum=sum+a[i]*5; instead of sum=sum+y*5;Finally, you are likely not to get the correct GPA output you want because you divided the total sum grades by the number of courses (variable n) rather than the sum of all course units in array a[] to get the GPA on line 42 gpa=sum/n;should be int x; 1 Like |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 11:49pm On Apr 13, 2015 |
kudaisi:Tnx a lot sir. I will effect the correction |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by kudaisi(m): 11:53pm On Apr 13, 2015 |
Pato5:You are welcome, let me know if there is anything else I can do to help. |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 12:38am On Apr 14, 2015 |
kudaisi:It worked. Pls can a function contain different variable data types i.e a float and integer in the same function? |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by kudaisi(m): 12:54am On Apr 14, 2015 |
yes. it can. 1 Like 1 Share |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 12:58am On Apr 14, 2015 |
#include<stdio.h> #include<math.h> int main() { double unit; float gpa; gpa=0; float sum; sum=0; int n; printf("enter number of courses:\n" scanf("%d",&n); char grade[n]; int a[n]; int i; for (i=0;i<n;i++) { printf("enter course%d grade|unit\n",i+1); scanf(" %c %ld",&grade[i],&a[i]); switch(grade[i]) { case 'A': sum=sum+5*a[i]; break; case 'B': sum=sum+4*a[i]; break; case 'C': sum=sum+3*a[i]; break; case 'D': sum=sum+2*a[i]; break; case 'E': sum=sum+1*a[i]; break; default: printf("invalid entry\n" break; } unit=unit+a[i]; } gpa=sum/unit; printf("s/n. grade|units\n" for (i=0;i<n;i++) { printf("%d.%c|%d\n",i+1,grade[i],a[i]); } printf("total credit unit is %lf\ntotal score is %f\n",unit,sum); printf("GPA=%f\n",gpa); return 0; }
|
Re: Please Help Me Check Whats Wrong With My Code(c Language) by kudaisi(m): 1:37am On Apr 14, 2015 |
Bad Guy. |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 6:54am On Apr 14, 2015 |
kudaisi:#include<studio.h> Int main(void) { /*My main target is to delve in embedded systems ( I'm an EE student). The prerequisite for embedded systems design/programming is c or asm. Programming can be boring especially when self tutorial. The only joy is when u are producing results*/ printf("Once again thanks for your help\n" return 0; } |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Nobody: 2:56pm On Apr 14, 2015 |
Pato5:I hope to see the gpa app. Use. Mosyn sdk to run the code 1 Like |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 4:02pm On Apr 14, 2015 |
proxy23:i will download it and see if i can grasp Software Development. will update my progress |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Singapore1(m): 11:38am On Apr 16, 2015 |
Pato5:my opinion though why dont you add if ( GPA>= 2.80) printf(" UPPER CREDIT" bla bla bla like that 1 Like 1 Share |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 12:47pm On Apr 16, 2015 |
Singapore1:ok i will add that... it's gonna be cool |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Booyakasha(f): 1:17pm On Apr 16, 2015 |
Guys, join Gdevit.com great spot for web developers and programmers... |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by nerdyorion: 3:03pm On Apr 16, 2015 |
An IT/Software company in Lagos which specializes in developing web and mobile applications desires to employ fresh graduates (Awaiting NYSC, Serving or recently completed NYSC) to fill available vacancies. - Strong knowledge of IT, preferably a background in computer science, engineering or other relevant disciplines - Must have basic programming knowledge and be ready to learn e.g. .NET, SQL Server, Mobile App Development - Must be intelligent, self-disciplined, confident and ready to work - Physically and mentally fit to perform under pressure - Must not be above 28 years Interested candidates should kindly send CV to info@mobilixe.com on or before Tuesday 21th of April 2015. |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by Pato5(m): 4:40pm On Apr 16, 2015 |
nerdyorion:Please what of one year internship for a computer science student? |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by danvery2k6(m): 2:26am On Apr 20, 2015 |
Pato5: this might work fine for you at this point because you are working with a program that does not require a high degree of accuracy. avoid declaring a variable as a float and then initializing it with an int if you do indeed delve into embedded systems like you propose to. it could produce rickety software my 1cent 1 Like |
Re: Please Help Me Check Whats Wrong With My Code(c Language) by kudaisi(m): 2:22pm On Apr 20, 2015 |
Just posted a Java GUI version of your GPA calculator https://www.nairaland.com/2264399/urgently-need-assistant-java-gui |
(1) (Reply)
Creating Your Own Game / Apart From Programming And Coding, What IT Related Career Is In High Demand? / How Does 2go And Related Phone Applications Really Work?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 51 |