-2

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've gotten some suggestions and even bits of code from you guys. Here is my code what is the best way to go about creating a loop that will search through all the students and find the oldest?

Thank you.

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

struct studentDataType
{
    int studentID;
    int year;
    int month;
    int day;
    long long phone;
};

int main (void)
{
    struct studentDataType *studentRecords=NULL;
    unsigned int students;
    unsigned int studentID;
    unsigned int year;
    unsigned int month;
    unsigned int day;
    unsigned long phone;

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

    studentRecords = malloc(sizeof(struct studentDataType) * students);
    int i=0;
    for (i; i != students ; ++i)  {
        printf("Enter information for student as follows (ID, DOB year, DOB month, DOB day, Phone): %d\n", i+1);
        struct studentDataType * s = &studentRecords[i];
        scanf("%u %u %u %u %u", &(s->studentID), &(s->year), &(s->month), &(s->day), &(s->phone));
    }
}
NSwanson7
  • 173
  • 1
  • 13
  • 3
    :-) I am following unanswered questions on Stack-Overflow, and after every ten minutes this same question and code arises with different title and different problem tell me if I am wrong. No offense but I would suggest you to make your basics clear and try on your own for basic problems, this is how you learn. (And when last time I added the answer for int declaration in 'for' loop then please, I expected at least an up-vote....:-) ;-)) – sumitb.mdi Nov 01 '13 at 16:52
  • 1
    Please stop posting duplicates of the same question ([here](http://stackoverflow.com/q/19728660/335858) and [here](http://stackoverflow.com/q/19730765/335858)). If you have not solved your original problem, please go back to the question and edit. If you did solve the previous problem, accept or delete the question. It is not a good idea to post code that you get from answers to your earlier questions as new questions. – Sergey Kalinichenko Nov 01 '13 at 16:53

1 Answers1

1

Start with initializing a local "date" with zeros (i.e. the smallest date possible). Then loop over all entries in the collection. When you find a structure with an older "date" than the local, save the index to that entry and set the local date to the entries date. Then when the loop is over, the saved index is of the "oldest" entry.

Something like this pseudo code:

oldest_date = 0;
oldest_index = -1;

loop_over_all_students
{
    if current_student.date > oldest_date
    {
        oldest_date = current_student.date
        oldest_index = current_index
    }
}

if (oldest_index >= 0)
{
    /* The variable `oldest_index` is the index to the "oldest" student */
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Could you give me some sample code? – NSwanson7 Nov 01 '13 at 16:49
  • 1
    @user2172993 Because [this question](http://stackoverflow.com/questions/19727857/do-i-need-to-create-three-separate-arrays-for-my-assignment), [this question](http://stackoverflow.com/questions/19728660/how-do-i-use-a-structure), [this question](http://stackoverflow.com/questions/19730765/why-isnt-this-program-allowing-me-to-enter-information-when-i-need-too), and now the current question hasn't gotten you enough? The very-code you posted for this question is pasted *verbatim* from someone's answer to a prior question. – WhozCraig Nov 01 '13 at 16:53
  • @user2172993 Addes pseudo-code – Some programmer dude Nov 01 '13 at 16:56