0

Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).

Here is my code, I havent done much in it yet because I'm not sure really where to start. I've already setup the dynamic memory allocation, but I don't know how to work the rest of this. Please help me.

Thank you.

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
int * studentData= NULL;

int students;
printf("How many students are you entering records for:\n");
scanf("%d", &students);

studentData=(int*)malloc((sizeof(int)*students));
}
NSwanson7
  • 173
  • 1
  • 13
  • 2
    Do you mean 3 seperate arrays one for ID, one for DOB and one for phone? If so, then yes you could do it that way. A nicer way might be to use a structure (http://en.wikipedia.org/wiki/Struct_(C_programming_language)). – Jimbo Nov 01 '13 at 13:39
  • Oh, also, in C it is normally a good idea to *not* cast the return value of malloc - http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Jimbo Nov 01 '13 at 13:40

2 Answers2

3

You could define a structure:

//Define a type, such as int, char, double...
typedef struct studentDataType {
    int ID;
    int birthDateDay;
    int birthDateMonth;
    int birthDateYear;
    int phoneNumber;
};

Then create an array, where each of those elements is of type studentData:

//Create an array, where each element is of type studentData
studentDataType *studentData = (studentDataType *)malloc(numberOfStudents * sizeof(studentData));

Then loop through them with:

for (int i = 0 ; i < numberOfStudents ; ++i) {
    printf("%i %i %i\n", studentData[i].ID, studentData[i].phoneNumber);
}
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
  • 1
    Semi-colon missing in `struct` declaration. – Sadique Nov 01 '13 at 13:43
  • 1
    `cout` is C++, not C. You will have to use `printf` – Jimbo Nov 01 '13 at 13:44
  • Sorry, use printf, cout is C++. – Lord Zsolt Nov 01 '13 at 13:44
  • I'm a little confused on what arrayOfStudents is referencing too. – NSwanson7 Nov 01 '13 at 13:51
  • The arrayOfStudents is the array which contains the data (in your code, I presume studentData is intended to be the same as my arrayOfStudents). – Lord Zsolt Nov 01 '13 at 13:53
  • Ok, thank you I was assuming that but as you can tell I'm still just getting my feet wet in the world of programming. Thanks for the help – NSwanson7 Nov 01 '13 at 13:54
  • @LordZsolt It doesn't necessary to cast return value of malloc(). Moreover it doesn't give any warnings at all. – Michael Nov 01 '13 at 13:55
  • No problem, you'll get used to it. The fact that you're dynamically allocating memory means you're doing quite well :) – Lord Zsolt Nov 01 '13 at 13:56
  • @LordZsolt I replaced *arrayOfStudents w/ *studentData in your second piece of code you provided me. But I'm getting the following error "invalid operands to binary." What do I need to change? Also if you wouldn't mind I would greatly appreciate it if you could change what you provided me with to C format because it's really confusing me lol. Thank you – NSwanson7 Nov 01 '13 at 14:00
  • @Michael Refer to this thread about why you should cast void* pointers: http://stackoverflow.com/questions/16986214/why-type-cast-a-void-pointer – Lord Zsolt Nov 01 '13 at 14:01
  • If you simply replace it, it will conflict since studentData is actually the type of your structure (two things have same name). Updated my code so the array is named studentData – Lord Zsolt Nov 01 '13 at 14:04
  • Glad I could help :) Don't forget to accept the answer, so others can see the question's been answered. – Lord Zsolt Nov 01 '13 at 14:07
  • @LordZsolt Here is a pure "C". And first answer at your reference link contains another link to a [question] (http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Michael Nov 01 '13 at 14:13
0

Use the following struct. You can make year, month and day as separate fields. It will be simpler for a quick start:

struct Student
{
    int studentID; 
    int year;
    int month;
    int day;
    long long phone; // phone is too large for 32 int
};
Michael
  • 1,505
  • 14
  • 26