Jump to content

Please help me about conversion to structure

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
chekers

chekers

    Newbie

  • Members
  • Pip
  • 3 posts
This is the guideline,,,my lecturer ask me to convert the given coding with more easiest coding...short coding with structures ..

like when i execute the coding given...its ask me to enter number of student..
i enter 3....then it ask me to enter name, then enter result for 3 subject and then enter gpa for the 3 subjects given...the same repeated for 3 student and the result of the gpa is given at the end ..the example is like below..

the coding is

Quote

#include <stdio.h>
#include <string.h>

void input_name(char name[][10], int stud, float mark_cs1[], float mark_cs2[], float mark_cs3[],int ch_cs1[],int ch_cs2[],int ch_cs3[],float gpa[]);
void input_mark(float mark1[], float mark2[], float mark3[], int i);
void calculate_gpa(float mark1[],float mark2[],float mark3[],int i,float gpa[],int ch_cs1[],int ch_cs2[],int ch_cs3[]);
void input_credithr(int ch_cs1[],int ch_cs2[],int ch_cs3[],int i);
void get_gradepoint(float *);

void main(void)
{

char name [10][10];
float mark_cs1[10];
float mark_cs2[10];
float mark_cs3[10];
int ch_cs1[10];
int ch_cs2[10];
int ch_cs3[10];
float gpa[10];
int i,no_stud=0;

printf("Enter number of student \n");
scanf("%d",&no_stud);
input_name(name,no_stud,mark_cs1,mark_cs2,mark_cs3,ch_cs1,ch_cs2,ch_cs3,gpa);


for (i=0; i<no_stud; i++)
printf("You have entered %s whose GPA is %.2f\n",name[i],gpa[i]);

printf("Thank you \n");

}

void input_name(char name[][10], int stud, float mark_cs1[], float mark_cs2[], float mark_cs3[],int ch_cs1[],int ch_cs2[],int ch_cs3[],float gpa[])
{
int i;

for (i=0; i<stud; i++)
{ printf("Enter a name \n");
scanf("%s",name[i]);
input_mark(mark_cs1,mark_cs2,mark_cs3,i);
input_credithr(ch_cs1,ch_cs2,ch_cs3,i);
calculate_gpa(mark_cs1,mark_cs2,mark_cs3,i,gpa,ch_cs1,ch_cs2,ch_cs3);;
}
}

void input_mark(float mark1[], float mark2[], float mark3[], int i)
{

printf("Enter mark for course 1, 2 and 3 \n");
scanf("%f %f %f",&mark1[i], &mark2[i],&mark3[i]);

}

void calculate_gpa(float mark1[],float mark2[],float mark3[],int i,float gpa[],int ch_cs1[],int ch_cs2[],int ch_cs3[])
{
float subgp1,subgp2,subgp3, totalcredit;

totalcredit = ch_cs1[i]+ch_cs2[i]+ch_cs3[i];
get_gradepoint(&mark1[i]);
subgp1 = mark1[i] * (float)ch_cs1[i];
get_gradepoint(&mark2[i]);
subgp2 = mark2[i] * (float)ch_cs2[i];
get_gradepoint(&mark3[i]);
subgp3 = mark3[i] * (float)ch_cs3[i];

gpa[i] = (subgp1+subgp2+subgp3)/totalcredit;
}

void input_credithr(int ch_cs1[],int ch_cs2[],int ch_cs3[],int i)
{
printf("Enter credit hour for course 1, 2 and 3 \n");
scanf("%d %d %d",&ch_cs1[i], &ch_cs2[i],&ch_cs3[i]);
}

void get_gradepoint(float *mark)
{ if (*mark > 80)
*mark = 4.0;
else if (*mark > 70)
*mark = 3.0;
else if (*mark > 60)
*mark = 2.0;
else if (*mark > 50)
*mark = 1.0;
else if (*mark > 0)
*mark = 0;
}


output is

Quote

Enter number of student
3
Enter a name
james
Enter mark for course 1, 2 and 3
45 56 78
Enter credit hour for dourse 1, 2 and 3
3 3 3
Enter a name
samuel
Enter mark for course 1, 2 and 3
56 87 88
Enter credit hour for course 1, 2 and 3
3 3 3
Enter a name
kevin
Enter mark for course 1, 2 and 3
78 89 90
Enter credit hour for course 1, 2 and 3
3 3 3
You have entered james whose GPA is 1.33
You have entered samuel whose GPA is 3.00
You have entered kevin whose GPA is 3.67
Thank you
Press any key to continue.


my task is to to have exactly same output but by using structure statements...
plz help me...i need to submit it in 2 days.....plz plz plz ...

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
We have a pretty well-adhered to unwritten rule about not doing homework, however we do help. I will help you by teaching you how to understand the concept of structures, and how they'll help clean your code.

A struct is a basic idea, it's that you can collect an assortment of independent data types (such as char, int, or float) and collect them into a single data type. The struct statement creates a sort of "plan" for instantiating a collection of data types. This way, you can actually declare multiple data types collected in a stack with a single statement, as well as place these structs in arrays like any other data type.

Most practical implementations of C structs use typedef to make defining struct objects a little more intuitive by not requiring the struct keyword in each instantiation, however for the purposes of this example I will not do so. Consider the following:
struct myStruct {

    float mark[10];

    int ch[10];

}


int main(void) {

    struct myStruct obj;

    /* You may now access fields in obj using the . operator */

    obj.mark[0] = 5.5;
This is the basic idea behind a struct. This allows for all sorts of advantages while programming, the biggest of which is the collection of ideas from a loose amalgamation into a solid collection. This also allows you to treat each "student" similarly using a struct Student. For example if you've got an array of Student structs, you could easily set all of those student's ID values, or GPA values, in a single for loop.

I hope this helped!
Wow I changed my sig!

#3
chekers

chekers

    Newbie

  • Members
  • Pip
  • 3 posts
i've done a little part in it...

Quote

#include <stdio.h>

int main(void)
{
char names[16][16] = {{0}};
double grades[16][5] = {{0}};
int credhrs[16][5] = {{0}};
int i;
int num_students = 0;

printf("Enter number of student \n");
scanf("%d", &num_students);

for( i = 0; i < num_students; i++ )
{
printf("Enter a name \n");
scanf("%s", names[i]);
getchar();
printf("Enter mark for course 1, 2 and 3 \n");
scanf("%lf%lf%lf", &grades[i][0], &grades[i][1] , &grades[i][2] );
getchar();
printf("Enter credit hour for course 1, 2 and 3 \n");
scanf("%d%d%d", &credhrs[i][0], &credhrs[i][1] , &credhrs[i][2] );
getchar();
}

return 0;
}

and the output is

Quote

Enter number of student
2
Enter a name
mesh
Enter mark for course 1, 2 and 3
23 34 45
Enter credit hour for course 1, 2 and 3
3 3 3
Enter a name
james
Enter mark for course 1, 2 and 3
3 44 45
Enter credit hour for course 1, 2 and 3
3 3 3
Press any key to continue


now i would like to know how to do the calculation part....like let say....

Quote

student mesh...course mark is 23,34,45 and cdt hour is 3,3,3.
i wan to get a gpa result...by adding all the coursemark =23+34+45 and then divide with the cdt hour=3+3+3
so course mark = 102
cdt hour = 9

and then divide = 102/9 =11.3


{ if (*mark > 80)
*mark = 4.0;
else if (*mark > 70)
*mark = 3.0;
else if (*mark > 60)
*mark = 2.0;
else if (*mark > 50)
*mark = 1.0;
else if (*mark > 0)
*mark = 0;
}

so student mesh gpa is 1.0 based on the if statement above as he get mark...11.3


#4
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
usually when u r doing the calculation part , i will suggest u to use cast data type which means ==> int i = 10;
(double) i;

hope u can understand lar..

zekedragon..i would like to ask about the typedef..if it encourages for us to use to declare an identifier eg.

int main()
{
typedef double haha_t;
haha_t average;

return 0;
}

something like tat..?

#5
chekers

chekers

    Newbie

  • Members
  • Pip
  • 3 posts
#include <stdio.h>


struct student_struct
{

char student_name[31];
int mark_cs1;
int mark_cs2;
int mark_cs3;
int ch_cs1;
int ch_cs2;
int ch_cs3;
int gpa;
};


void input_student_data(struct student_struct *st_rec);

int computed_gpa(int average);


int main(void)
{
	struct student_struct student_record;
	
		{
		input_student_data(&student_record);


		printf("student name   :%s\n",student_record.student_name);
		printf("with gpa  :%c\n",(student_record.gpa));

		

	}
	return 0;

}

void input_student_data(struct student_struct *s)
{

	
	printf("enter student name   :");
	gets(s->student_name);
	printf("enter mark for course 1  :");
	scanf("%d",  &s->mark_cs1);
	printf("enter mark for course 2 :");
	scanf("%d",  &s->mark_cs2);
	printf("enter mark for course 3 :");
	scanf("%d",  &s->mark_cs3);
	printf("enter credit hour for course 1 :");
	scanf("%d",  &s->ch_cs1);
	printf("enter credit hour for course 2  :");
	scanf("%d",  &s->ch_cs2);
	printf("enter credit hour for course 3  :");
	scanf("%d",  &s->ch_cs3);
}


  	int computed_gpa(int gpa)
		{
			char grade;

			if(gpa >=90)
				grade = '4';
			else if(gpa >=80)
				grade = '3';
			else if(gpa >=70)
				grade = '2';
			else if(gpa >=60)
				grade = '1';
			else
				grade = '0';
			return grade;
		}


plz help me with the calculation part...

like i enter :
mark course 1= 45
mark course 2=67
mark course 3= 88
cdt hour for course 1= 3
cdt hour for course 2= 2
cdt hour for course 3= 4


so the calculation will go like this :
mark course 1* cdt hour course1 +mark course 2* cdt hour course2+mark course 3* cdt hour course3 / total cdt hour of course 1,2,3 ..

and it follow this rule
char grade;

			if(gpa >=90)
				grade = '4';
			else if(gpa >=80)
				grade = '3';
			else if(gpa >=70)
				grade = '2';
			else if(gpa >=60)
				grade = '1';
			else
				grade = '0';
			return grade;
		}


where in output....it will show the gpa.... :) plz help me....i've done something rather then nothin....i need to submit it tomorrow...please...Thanks alot again...

Edited by WingedPanther, 21 October 2009 - 08:07 AM.
change quote tags to code tags


#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
student.gpa = (student.mark_cs1 * student.ch_cs1 + student.mark_cs2 * student.ch_cs2 + student.mark_cs3 * student.ch_cs3)/(student.ch_cs1 + student.ch_cs2 + student.ch_cs3)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
this should do u the trick
hope u like it..coz i alter some of it ..and sorry for that
hopefully u can keep on practicing it..programming aim not hard but aim for sure that the person is a lazy bug~ because the person dun debugging the bug so that is why the person is getting a credit C in his/her C-programming
if u wan some tips on how i have learned my programming so far until now u can ask me ..~ and this is already 1 semester (maybe less than 5 month) i have learned it .. hehe .. im going to sit for my exam the following two weeks~
and im going to learn my java and my C++ right after my final finish maybe during my holiday~ miss my home now~ huhu~
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student_struct
{

char student_name[31];
double mark_cs1;
double mark_cs2;
double mark_cs3;
double ch_cs1;
double ch_cs2;
double ch_cs3;
double gpa;
char grade;
};


void input_student_data(struct student_struct *s);

void computed_gpa(struct student_struct *s);
void calculate_gpa(struct student_struct *s);


int main(void)
{
	struct student_struct student_record;
	
    input_student_data(&student_record);
    printf("student name   :%s\n",student_record.student_name);
    printf("with gpa  :%.2lf\n",student_record.gpa);
    printf("student grade :%c\n", student_record.grade);
    
	return 0;

}

void input_student_data(struct student_struct *s)
{

	
	printf("enter student name   :");
	fgets(s->student_name , 31 , stdin);
    s->student_name[strlen(s->student_name)-1] = '\0';
	printf("enter mark for course 1  :");
	scanf("%lf",  &s->mark_cs1);
	printf("enter mark for course 2 :");
	scanf("%lf",  &s->mark_cs2);
	printf("enter mark for course 3 :");
	scanf("%lf",  &s->mark_cs3);
	printf("enter credit hour for course 1 :");
	scanf("%lf",  &s->ch_cs1);
	printf("enter credit hour for course 2  :");
	scanf("%lf",  &s->ch_cs2);
	printf("enter credit hour for course 3  :");
	scanf("%lf",  &s->ch_cs3);
    calculate_gpa(s);
}

void calculate_gpa(struct student_struct *s)
{
    double temp1;
    double temp2;
    double temp3;
    double total_credit;
    temp1 = (double)(s->mark_cs1)*(double)(s->ch_cs1);
    temp2 = (double)(s->mark_cs2)*(double)(s->ch_cs2);
    temp3 = (double)(s->mark_cs3)*(double)(s->ch_cs3);
    
    total_credit = (double)(s->ch_cs1) + (double)(s->ch_cs2) +(double)(s->ch_cs3);
    s->gpa = (temp1 + temp2 + temp3) / (total_credit);
    computed_gpa(s);
}

void computed_gpa(struct student_struct *s)
{
    if((s->gpa) >=90.00)
    {
        s->grade = '4';
    }
    else if((s->gpa) >=80.00)
    {
        s->grade = '3';
    }
    else if((s->gpa) >=70.00)
    {
        s->grade = '2';
    }
    else if((s->gpa) >=60.00)
    {
        s->grade = '1';
    }
    else
	{	
        s->grade = '0';
    }
    
}

Edited by R3.RyozKidz, 21 October 2009 - 10:02 AM.
missed out somethings