Please help me about conversion to structure

Discussion in 'C' started by chekers, Oct 18, 2009.

  1. chekers

    chekers New Member

    Joined:
    Oct 18, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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
    Code:
    #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

    Code:
    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. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Simply place the definitions you want into a structure. Then create a variable whose type is that structure. Then to access members you just use the variable name, a dot or arrow depending on whether you're using the structure directly or via a pointer, then the member. Instead of passing in all the members of a structure individually to a function as the code does now, you pass in the structure itself or a pointer to it.

    For example, using individual variables to represent a complex number:
    Code:
    int re, im;
    char buf[32];
    printf("Enter real value :"); fgets(buf,30,stdin); re=atoi(buf);
    printf("Enter imag value :"); fgets(buf,30,stdin); im=atoi(buf);
    somefunc(re,im);
    
    // ...
    void somefunc(int re,int im)
    {
      re=<whatever>;
      im=<whatever>;
    }
    
    Same code using a structure:
    Code:
    struct cmplx
    {
      int re,im;
    };
    struct cmplx foo;
    printf("Enter real value :"); fgets(buf,30,stdin); foo.re=atoi(buf);
    printf("Enter imag value :"); fgets(buf,30,stdin); foo.im=atoi(buf);
    somefunc(foo, &foo);
    
    // ...
    void somefunc(struct cmplx bar, struct cmplx *gronk)
    {
      // modifies local copy
      bar.re=<whatever>;
      bar.im=<whatever>;
    
      // modifies original copy, i.e. foo, via the pointer
      gronk->re=<whatever>;
      gronk->im=<whatever>;
    }
    
     
    Last edited: Oct 18, 2009
  3. chekers

    chekers New Member

    Joined:
    Oct 18, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    i've done a little part in it...

    Code:
    #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

    Code:
    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....

    Code:
    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
    so that the new output will be like this
    Code:
    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
    You have entered mesh whose GPA is 1.00
    You have entered james whose GPA is 1.33
    Thank you
    Press any key to continue
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What's that got to do with conversion to structure?
    Has the assignment changed?
     
  5. chekers

    chekers New Member

    Joined:
    Oct 18, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    nope..didnt changed...i guess i ruined the coding... :( ..
    in my group of 3 gals.....we are just being so stressed because we've given so many assignments...still got another 4 to go......we didnt learn about structure in c yet....thats y we seriously need a coding so that we can learn it.....


    i know everyone here is so professionals in c programming and i admit myself...we are noob here and we can learn by having a exact coding by re do it without refering :( im sorry again
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's OK, don't worry about it. Focus on just one task at a time and make sure you take some time off. Programming is a difficult task and takes time to get used to. Grab a cup of coffee and read through this lot:

    A structure is actually quite simple. It's just a wrapper for individual simple data types.

    So to take my "complex number" example, we can represent it as two integers (actually you'd want floating point, but let's not overcomplicate it for now):
    Code:
    int re;
    int im;
    
    If you're not sure what complex numbers are, don't worry about that; it's like co-ordinates; it takes two ordinary numbers (called "real" and "imaginary") to represent a complex number.

    The problem with the above definition is that re and im aren't linked in any way and if this were extended to larger data structures like the one you're using, it would be easy to get confused as to how the individual items are related to each other and to what larger design concept any individual variable belongs to. So we have the concept of data structures, and that's what you're looking at here.

    A structure is easily created; just wrap the above with "struct struct_name { ... } var_name" where struct_name is a name for the structure and var_name is a variable name that "contains" the structure. Both struct_name and var_name are optional, but you need at least one of them.

    So we can create a named structure, called cmplx, which represents complex numbers, as follows:
    Code:
    struct cmplx
    {
      int re;
      int im;
    };
    
    Effectively this creates a new data type. We can create variables of this new data type as follows:
    Code:
    struct cmplx foo; // a single item
    struct cmplx bar[10]; // array of 10 cmplx's
    
    A slight complication in the example you've been given is that each item is an array of 10, so you need to decide if the structure should contain these arrays, or if a structure should be of a single item and you have an array of 10 structures. I would suggest the latter, and therefore you might get something like:
    Code:
    struct student
    {
    	char name [10];
    	float mark_cs1;
    };
    struct student student_array[10];
    
    The input_name function call changes to something like this:
    Code:
    input_name(no_stud, &student_array[0]);
    
    and the function definition to something like this:
    Code:
    void input_name(int stud, struct student_array *sa)
    {
    	int i;
    
    	for (i=0; i<stud; i++)
    	{ printf("Enter a name \n");
    	  scanf("%s",sa->name[i]);
    ...
    
    So we pass the address of the first element of student_array to the function, and as this is a pointer, in the function we need to use "sa->" to access individual members of the structure.

    There's no need to rewrite any of the code; just enclose the appropriate parts in a struct { ... } block, then change the function definitions, then change the references within the functions (sa->) as I've shown above. Just stay focussed on what you need to do. If changing the code is the subject of a later assignment, forget that for now and just focus on the struct stuff.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Programmer's Rule #1: KISS (Keep It Simple, Stupid).
     
  8. chekers

    chekers New Member

    Joined:
    Oct 18, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #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
    Code:
    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...
     
    Last edited by a moderator: Oct 21, 2009
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I suggest you build the expression up bit by bit. Make sure you understand clearly how to calculate the result and see if you can write it down as an expression, e.g. mark course 1* cdt hour course1 would be 45 * 3 in the data you've given. Perhaps it would help to create a small testbed to get this expression sorted out so that you're not messing around re-entering loads of data on every run. You could do something like:
    Code:
    int main()
    {
      int mark1=45, mark2=67, mark3=88;
      int cdt1=3, cdt2=2, cdt3=4;
    
      int gpa= ... // this is what you're trying to figure out
      printf("Calculated mark: %d\n",gpa);
      return 0;
    }
    
    Make sure this matches what you've worked out on paper then when a few examples match what you worked out separately you can then copy the gpa expression into the main program, confident that it'll work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice