C GPA Gross Point Average [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am extremely new to C I am trying to write a simple program that will prompt the user to enter a number of modules followed by credit and numeric grade for each module, this program must print out the information received by the user including the letter grade for each module and the GPA at the bottom. I have got to the point where I am trying to convert numeric grade to a letter grade and I am trying to write a class that I can input the numeric grade say "mod1.grade" that will return the letter grade but cannot work this out as I said I am a complete newby so would be gratefull of any help received.
#include <stdio.h>
#include <string.h>
struct module { char moduleid[10]; int credit; float grade; };
int main( ) {
struct module mod1,mod2; printf("Please enter: module id, module credit and module grade\n");
scanf("%s%d%f",mod1.moduleid,&mod1.credit,&mod1.grade);
scanf("%s%d%f",mod2.moduleid,&mod2.credit,&mod2.grade);
String getGrade (float input){
String letterGrade;
if(input >= 80&&<=100){
letterGrade = 'A';
}
return letterGrade;
}
printf( "Module id\tCredit\t\tGrade\n");
printf("%s\t\t%d\t\t%f\t%s\n",mod1.moduleid,mod1.credit,mod1.grade,getGrade(mod1.grade));
printf( "%s\t\t%d\t\t%f\n",mod2.moduleid,mod2.credit,mod2.grade);
return 0;
}

Your code should be:
#include <stdio.h>
#include <string.h>
struct module {
char moduleid[10];
int credit;
float grade;
};
char getGrade (float input){ //you need to return a char
//there isn't any "String" data type in C
char letterGrade; //char not String
if(input >= 80&&input<=100){ //note the difference here
letterGrade = 'A';
}
return letterGrade;
}
int main( ) {
struct module mod1,mod2;
printf("Please enter: module id, module credit and module grade\n");
scanf("%s%d%f",mod1.moduleid,&mod1.credit,&mod1.grade);
scanf("%s%d%f",mod2.moduleid,&mod2.credit,&mod2.grade);
printf( "Module id\tCredit\t\tGrade\n");
printf("%s\t\t%d\t\t%f\t%c\n",mod1.moduleid,mod1.credit,mod1.grade,getGrade(mod1.grade));//%c for a character
printf( "%s\t\t%d\t\t%f\t%c\n",mod2.moduleid,mod2.credit,mod2.grade,getGrade(mod2.grade)); //same here too
return 0;
}

Related

convert string to double , the answer is correct ,but not very accurate [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to use atof to convert my string to a double,the answer is correct ,but not very accurate
ATTENTION: because of some other reasons, fscanf is not permitted
my code is :
#include <stdio.h>
#include <stdlib.h>
#define MAXCN 50
int main(void)
{ FILE* lstm_txt = NULL;
char lstm_weight[MAXCN] = {0};
int lstm = 0;
int i = 0;
float lstm_val;
if ((lstm_txt = fopen("test1.txt", "r"))== NULL){
fprintf(stderr,"error:file open failed 'test1.txt'.\n");
return 1;
}
while ((i + 1 < MAXCN) && ((lstm = fgetc(lstm_txt)) != ' ' ) && (lstm != EOF)){
lstm_weight[i++] = lstm;
}
//lstm_weight[i] = 0;
printf("\n lstm_weight: %s\n\n", lstm_weight);
lstm_val = atof(lstm_weight);
printf("\n convert \"lstm_weight\" to lstm_val is : %f\n\n", lstm_val);
return 0;
}
my file : lstm_txt is :
4.217959344387054443e-01 -2.566376626491546631e-01 2.173236161470413208e-01 4.217959344387054443e-01
code hasn't bug, and the result is :
lstm_weight: 4.217959344387054443e-01
convert "lstm_weight" to lstm_val is : 0.421796
but I want Istm_val is 0.4217959344387054443 ,how can I do that?
you could try something like sprintf()
here's an example:
#include <stdio.h>
int main() {
char str[50];
double n = 0.3984092590879;
sprintf(str, "%lf", n);
printf(str);
return 0;
}
prints out:
0.3984092590879
Printing %.17f you can have a precision up to 0.42179593443870544
printf("\n convert \"lstm_weight\" to lstm_val is : %.17f\n\n", lstm_val);
A double typically has about 15 digits of decimal precision. You will not get more accuracy than that if you store the value in a double. You are getting less because you didn't tell printf how many digits of precision to use for output, so you got the default.
Use something like %0.15f instead of %f.
convert "lstm_weight" to lstm_val is : 0.421795934438705
With %0.20f, I get:
convert "lstm_weight" to lstm_val is : 0.42179593443870544434
That's the best you'll do with a double.

Converting a signed char to an int in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Im having an issue converting a signed string into an int.
char *crn1, *crn2, *credit1, credit2;
char course1, course2;
crn1=strtok(course1,"/");
credit1=strtok(NULL,"/");
crn2 = strtok(course2,"/");
credit2 = strtok(NULL,"/");
Im trying to convert the signed char credit1 or credit2 to an integer for math used later on in my code. I either get a huge number or an error.
Use strtol (which is the safer version of atoi).
#include <stdlib.h>
#include <stdio.h>
int main() {
char *str = "5";
int n;
n = strtol(str, NULL, 10);
printf("n+1 is %d\n", n+1);
}
Using atoi:
#include <stdlib.h>
#include <stdio.h>
int main() {
char *str = "1";
int n;
n = atoi(str);
printf("n+1 is %d\n", n+1);
}
If instead of converting the digits in the string into an integer, you'd rather use the numerical value of the character, you can:
#include <stdio.h>
int main() {
char c = 'a';
printf("ascii code of %c is %hhu\n", c, c);
printf("after %c is %c with ascii code %u", c, c+1, c+1);
return 0;
}

with in C, access variable in structure [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have the following code, which is not how to implement in C, (if it has one, the keyword with.On the other hand because the program does not access the data type structure, it should not give an error because the variables partial_n2, final_n2, name2 are defined in the struct.
The program has to store in an array of records the names of the students, their partial and final grades. Find the average grade and show a message of SUIT if the student exceeds or equals the grade of 5 or NOT SUIT if it is not enough. Do it for a number of 5 students.
#include <stdio.h>
#include <windows.h>
#include <conio.h>
//PROGRAM EJER009
#define numstudents 5
typedef struct notas{
char name2[20];
float partial_n2, final_n2;
}tnotas;
tnotas notas[numstudents];
tnotas clase;
char name[20];
float partial_n, final_n, n_media;
int i;
int main(){
for (i = 0; i <= numstudents;i++)
{
printf("Enter the student's name% d: ",i);
scanf("%s",name);
printf("Enter your partial note: ");
scanf("%f",&partial_n);
printf("Enter your final note: ");
scanf("%f",&final_n);
printf("\n");
with (clase[i])
{
partial_n2 = partial_n;
final_n2 = final_n;
name2 = name;
}
}
printf("cls");
printf("NAME\tPartial\tFinal\tMedia\tQUALIFICATION\n");
for (i = 1; i<=numstudents;i++){
with clase[i]
{
n_media = (partial_n2 + final_n2) / 2;
printf("%d %d %d",name2,partial_n2,final_n2);
system("color 14"); printf("%lf",n_media);
if (n_media >= 5)
{
system("color 11");
printf("SUITABLE :-)");
}
else
{
system("color 1");
printf("NOT SUITABLE :-(");
}
system("color 7");
}
}
getch();
return 0;
}
You can read a value of a member by:
float f;
f = notas[0].partial_n2;
You can write a value of a member by:
notas[0].partial_n2 = 10.3;

How to read the list of number in c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a file which includes product name, product quantity and product price in the following format.
file.txt
3
Product Qty Price
Tv 2 10
Mobile 3 20
Computer 5 30
I want to read the number of products from the integer (such as 3) given above the product list and count the total price of the products. The program will use struct to read the product details such as
struct product {
Char name[30];
int qty ;
float price;
}
What are the best practices to make this program easier?
Please try if the following program can help you.
#include <stdio.h>
#include <stdlib.h>
struct product {
char name[30];
int qty;
float price;
};
int main(void) {
int count = 0;
char line[100];
FILE *fptr;
fptr = fopen("file.txt", "r");
fscanf(fptr, "%d", &count); // count = 3
struct product *p = malloc(sizeof(struct product));
int i = 0;
double sum = 0;
while (i < count + 2 && fgets(line, sizeof(line), fptr) != NULL) {
if (i > 1) {
sscanf(line, "%s %d %f\n", (*p).name, &(*p).qty, &(*p).price);
sum = sum + (*p).price;
}
i++;
}
printf("sum: %f\n", sum);
free(p);
return 0;
}
Test
$ gcc main.c
$ ./a.out
sum: 60.000000
$

How to loop for Structures? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
loops for structures
main()
{
structure perdata //MADE A STRUCTURE
{
char name;
int age;
float salary;
}p1,p2,p3;
int i;
for(i=1;i<4;i++)
{
printf("p%d.name",i);
scanf("%s",);/*should loop and read names of p1,p2,p3*/
}
printf("p1.name:%s",p1.name);
getch();
}
You should use array of structures so that you may iterate through it.
Example : An array of student structure,
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s[10]; //Ten student details maybe stored
int i;
printf("Enter information of students:\n");
//get the details of 10 students
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("\nFor roll number %d\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
//display the details of 10 students
printf("Displaying information of students:\n\n");
for(i=0;i<10;++i)
{
printf("\nInformation for roll number %d:\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}
Use the above sample code & write your program.
Always make arrays of structures, classes etc. whenever you need to loop through instances.
int main(void)
{
struct perdata //MADE A STRUCTURE
{
char name[20];
int age;
float salary;
};
struct perdata p[4];
for(int i=0;i<4;i++)
{
//printf("p%d.name",i);
scanf("%s", &p[i].name);/*should loop and read names of p1,p2,p3*/
}
for(int i=0;i<4;i++)
{
printf("p%d.name:%s\n",i+1, p[i].name);
}
getch();
}

Resources