GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Oct-2009, 10:55
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Convert to structure


anyone can help me to convert this c coding to a structure...i dont know how to do it...Plz help me....

CPP / C++ / C 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;
}


  #2  
Old 13-Oct-2009, 12:17
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Convert to structure


One doesnt generally convert code to structure. Can you elaborate on what exactly it is you need?
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 13-Oct-2009, 23:55
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Convert to structure


Create a structure (student) containing these fields
CPP / C++ / C Code:
	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];
making the structure an array instead of the fields. Then access each field by, for example, student[i].ch_cs1 instead of ch_cs1[i].

Most function calls can be simplified by passing the structure instead of each array as you do now.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #4  
Old 15-Oct-2009, 05:51
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


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



Anyone can help me to create the same output using own coding...difference from the one i gave above...thank you alot frens....
  #5  
Old 15-Oct-2009, 07:42
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


This is the question my lecturer gave to me...

Write a program that will read the name of a student and the three marks from three different courses entered by a user. For each course the credit hour is specified as 2, 3 or 4. The program continues to take these inputs until the maximum number of student (the maximum number is determined by the user). Store these inputs in the array. Calculate the GPA for each student based on the Grade Point below:


Marks Range Grade Grade point
80 - 100 A 4

70 - 79 B 3

60 - 69 C 2

50 - 59 D 1

< 49 F 0


Example:
Course A 87 A 3 credit hour
Course B 77 B 3 credit hour
Course C 65 C 3 credit hour
So, GPA = ((3 X 4) + (3 X 3) + (3 X 2)) / 9 = 3.00
  #6  
Old 15-Oct-2009, 15:43
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Convert to structure


Try to think of the objects involved:

A STUDENT has a name three courses. Each COURSE has CREDIT HOURS and a MARK. So:

CPP / C++ / C Code:
struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

struct Student
{
  char Name[32]; // Up to 31 characters long
  Course Courses[3]; // An array of 3 courses
};

// A few generic functions to show you how to access the structure
void PrintName(Student *student)
{
  printf(student->Name);
}

int AddMarks(Student *student)
{
  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}

int main()
{
  Student s;
  strcpy(s.Name, "chekers");
  
  s.Courses[0].CreditHours = 3;
  s.Courses[0].Mark = 68;

  s.Courses[1].CreditHours = 2;
  s.Courses[1].Mark = 55;

  s.Courses[2].CreditHours = 4;
  s.Courses[2].Mark = 81;

  printf("Sum of Marks = %d", AddMarks(&s));

  return 0;
}
  #7  
Old 15-Oct-2009, 21:19
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


Quote:
Originally Posted by fakepoo
Try to think of the objects involved:

A STUDENT has a name three courses. Each COURSE has CREDIT HOURS and a MARK. So:

CPP / C++ / C Code:
struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

struct Student
{
  char Name[32]; // Up to 31 characters long
  Course Courses[3]; // An array of 3 courses
};

// A few generic functions to show you how to access the structure
void PrintName(Student *student)
{
  printf(student->Name);
}

int AddMarks(Student *student)
{
  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}

int main()
{
  Student s;
  strcpy(s.Name, "chekers");
  
  s.Courses[0].CreditHours = 3;
  s.Courses[0].Mark = 68;

  s.Courses[1].CreditHours = 2;
  s.Courses[1].Mark = 55;

  s.Courses[2].CreditHours = 4;
  s.Courses[2].Mark = 81;

  printf("Sum of Marks = %d", AddMarks(&s));

  return 0;
}


I like your coding bro Fakepoo....but i dunno which header to use....i try execute in my vb6 and shows errors...and the file i must save for the assignment is in (.c)

I hope i can view the full start and end of the coding to make some studies on it...

Thanks alot fakepoo.........i really appreciate it
  #8  
Old 16-Oct-2009, 07:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Convert to structure


In order to compile, you will need to include string.h (for strcpy)and stdio.h (for printf).

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>


struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

struct Student
{
  char Name[32]; // Up to 31 characters long
  Course Courses[3]; // An array of 3 courses
};

// A few generic functions to show you how to access the structure
void PrintName(Student *student)
{
  printf(student->Name);
}

int AddMarks(Student *student)
{
  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}

int main()
{
  Student s;
  strcpy(s.Name, "chekers");
  
  s.Courses[0].CreditHours = 3;
  s.Courses[0].Mark = 68;

  s.Courses[1].CreditHours = 2;
  s.Courses[1].Mark = 55;

  s.Courses[2].CreditHours = 4;
  s.Courses[2].Mark = 81;

  printf("Sum of Marks = %d", AddMarks(&s));

  getchar();
  return 0;
}

Could the problem be that you are trying to compile a C program with a Visual Basic compiler?
  #9  
Old 16-Oct-2009, 09:56
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Convert to structure


Yes bro fakepoo...im using visual basic 6.0 because in my course ,we using this compiler and save the file in .c format...

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>


struct Course
{
  int CreditHours; // 2, 3, or 4
  int Mark; // 0 to 100
};

struct Student
{
  char Name[32]; // Up to 31 characters long
  Course Courses[3]; // An array of 3 courses
};

void PrintName(Student*student);
{
  printf(student->Name);
}

int AddMarks(Student *student);
{
  int sum = 0;
  for(int i=0;i<3;++i) sum += student->Courses[i].Mark;
  return sum;
}

int main()
{
  Student s;
  strcpy(s.Name, "chekers");
  
  s.Courses[0].CreditHours = 3;
  s.Courses[0].Mark = 68;

  s.Courses[1].CreditHours = 2;
  s.Courses[1].Mark = 55;

  s.Courses[2].CreditHours = 4;
  s.Courses[2].Mark = 81;

  printf("Sum of Marks = %d", AddMarks(&s));

  getchar();
  return 0;
}



The errors shown is :
Quote:
Compiling...
g.c
C:\Users\Viper\Desktop\g.c(14) : error C2061: syntax error : identifier 'Course'
C:\Users\Viper\Desktop\g.c(15) : error C2059: syntax error : '}'
C:\Users\Viper\Desktop\g.c(17) : error C2143: syntax error : missing ')' before '*'
C:\Users\Viper\Desktop\g.c(17) : error C2143: syntax error : missing '{' before '*'
C:\Users\Viper\Desktop\g.c(17) : error C2059: syntax error : ')'
C:\Users\Viper\Desktop\g.c(18 ) : error C2449: found '{' at file scope (missing function header?)
C:\Users\Viper\Desktop\g.c(20) : error C2059: syntax error : '}'
C:\Users\Viper\Desktop\g.c(23) : error C2449: found '{' at file scope (missing function header?)
C:\Users\Viper\Desktop\g.c(27) : error C2059: syntax error : '}'
Error executing cl.exe.

g.exe - 9 error(s), 0 warning(s)
  #10  
Old 16-Oct-2009, 10:05
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Convert to structure


Try compiling this:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  printf("Hello World");
  getchar();
  return 0;
}
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Structure with in a structure knockout_artist C Programming Language 3 19-Dec-2007 12:08
Allocating memory to a structure Printisor C Programming Language 4 29-Jun-2007 09:00
Convert the input into square yards pjpav Java Forum 1 08-Oct-2005 08:43
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.