5 Replies - 656 Views - Last Post: 15 December 2008 - 08:35 PM Rate Topic: -----

#1 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

user define type

Posted 15 December 2008 - 04:27 PM

My C teacher posted an assignment for us to do I am not really all that sure as to what he wants if someone code please try to translate it for me or help explain it better some sample code or even links to help me better understand this would be much apperciated im not asking anyone to do my homework I just want to be able to understand what excactly he is asking me to do


"Write a program that has a user define type “student”. The student type should
have three fields: name (character array of size 10), midterm (floating point
number) and final (floating point number).
Declare an array of type student with size 3. Use a for loop to initialize the array
and another for loop to print a report of each student’s average grade.
Program not using structure gets 0 points."

i am not sure what he means by a program that has user define type and an array of type student im not gettint that but

i have a little code i have been working on but i do not think i am doing it correctly
 
#include <stdio.h>


typedef struct {
char* first_name[10];
} student;
typedef struct {
float* mid_term;
} mid;
typedef struct {
float final_grade;
}final;
char name (student);
int main ()
{
int max;
student stu;
mid dim;
final nal;
printf("What is the students name?\n");
scanf("%s", &stu.first_name);
printf("What is the students Midterm Grade\n");
scanf("%f", &dim.mid_term);
printf("What is the students final grade?\n");
scanf("%f", &nal.final_grade);
name(max);
}
char name (student)
{
int i;
student max;
for (i = 0; i < 10; i++)
{
	max;
}
return(max);
}


i know the code is a mess and does not work i was trying to pass the array of student in the struct to a function to loop it then print out what the user typed in for the array not sure if thats what is meant though

Is This A Good Question/Topic? 0
  • +

Replies To: user define type

#2 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: user define type

Posted 15 December 2008 - 04:40 PM

You have three separate structs. What the assignment means is a single struct with those three variables in it. Then you just declare an array of 'student' like any other variable.

typedef struct
{
	char name[10];
	float midterm;
	float final;
} student;

...

student stu[3];  /* declares an array of students */


Was This Post Helpful? 0
  • +
  • -

#3 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

Re: user define type

Posted 15 December 2008 - 04:48 PM

another thing to add to my post as if it isnt long enough already could some explain how to pass a struct to a function like

name [10]

to


this function
{
int i;
student max;
for (i = 0; i < 10; i++)
{
	max;
}
return(max);
}


Was This Post Helpful? 0
  • +
  • -

#4 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: user define type

Posted 15 December 2008 - 04:54 PM

You have the option of either passing an entire struct or just one data member.

/* passing the whole struct */
void function(student stu); /* function declaration */
function(stu); /* function call */

/* passing a member of the struct */
void function(char name[10]); /* function declaration */
function(stu.name); /* function call */



When passing a member of the struct, the function header will contain the member's data type (it doesn't care if the data is part of a struct or not) and then you just pass it using the member access operator (the dot).
Was This Post Helpful? 0
  • +
  • -

#5 dbthegrandtour  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 15-December 08

Re: user define type

Posted 15 December 2008 - 05:38 PM

so ive gotten this far
#include <stdio.h>

typedef struct
{
	char name[10];
	float midterm;
	float final;
} student;
char function(char name[10]); 
int main ()
{
student stu;
	function(stu.name);
}
char function(char name[10])
{
	scanf("%s", &name[2]);
	printf("%s", name[2]);
return (0);
}


and still getting errors when trying to pass the functions around just in time error
Was This Post Helpful? 0
  • +
  • -

#6 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: user define type

Posted 15 December 2008 - 08:35 PM

You're not using the character array correctly in your function. You're trying to work with one character instead of the whole string. Also, you shouldn't return anything in your function. Just make it void and return 0 in main.

void function(char name[10])
{
	scanf("%s", name);
	printf("%s", name);
}


This post has been edited by Darkhack: 15 December 2008 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1