1

I'm working on an assignment for storing numbers and names into an array. We have to store 4 numbers to represent time in seconds into each number in the array. This is then totaled and output for each number in an English sentence--in minutes and seconds format.

Here's what I have, which is very minimal and I'm not even sure it does what it's supposed to:

#include <stdio.h>

int main()
{
    int i = 0; 
    int person[8] = { 0 };

    int leg1;
    int leg2;
    int leg3;
    int leg4;

    while (i < 8) {
        printf("Hello, please enter the number of the person.:\n"); 
        scanf("%d", &person[i]); 
        i = i + 1;
    }

    printf("%d ", i);
    return 0;
}

Ideally instead of numbers I'd like to record a name for each person in the array - a total of 8 names but even the numbers I don't think work.

I'm not sure if its possible to then assign each number in the code 4 values of time using scanf.

The legs are there to store the 4 values of time and then I would sum them up and convert it to seconds and output the time for each value as a whole. I just don't know how to do it for arrays.

edit: not very used to how editing works on this, half of what I wrote is gone but that's fine most of it was all mumbo jumbo anyway. Heres the other bit of code I have that records the time and categorizes the times:

#include <stdio.h>

int main()
{
    char name[31]; 
    int leg1;
    int leg2; 
    int leg3;
    int leg4;
    int totalTime;

    printf("Hello, please enter the name of the person.:\n");
    scanf("%30s", &name);

    printf("Now please enter the time of the person for the first leg in seconds.:\n");
    scanf("%d", &leg1); 

    printf("Now please enter the time of the person for the second leg in seconds.:\n"); 
    scanf("%d", &leg2); 

    printf("Now please enter the time of the person for the third leg in seconds.:\n");
    scanf("%d", &leg3); 

    printf("Now please enter the time of the person for the final leg in seconds.:\n");
    scanf("%d", &leg4); 

    totalTime = leg1 + leg2 + leg3 + leg4; 

    int minutes = totalTime / 60;
    int seconds = totalTime % 60; 

    if (minutes < 4)
    {
        printf("\t%30s qualified for the International Tournament with a time of %d minutes and %d seconds", name, minutes, seconds);
    }
    else if (minutes >= 4 && minutes < 12)
    {
        printf("\t%30s qualified for the Natonal Race Meeting with a time of %d minutes and %d seconds", name, minutes, seconds);
    }
    else if (minutes >= 12 && minutes < 30)
    {
        printf("\t%30s qualified for the Beginner's League with a time of %d minutes and %d seconds", name, minutes, seconds);
    }
    else if (minutes >= 30)
    {
        printf("\t%30s did not qualify for any league with a really shit time of %d minutes and %d seconds", name, minutes, seconds);
    }

    return 0;

}
Hamza A
  • 13
  • 3
  • 1
    `I do already have code that has the user input time and store each value of time separately. It then sums the seconds and prints it in minutes and seconds format. I just need to do this for each array value using loops` I don't see such code in the question did you forget to include in the question? And problems like this will be solved more better with `structures`. Are you familiar with `structures` ? – kiran Biradar Nov 10 '18 at 16:52
  • oops sorry, seems I forgot to add the code, just got out of the shower and am about to head out, I'll edit it in now, its very basic and convoluted but it got the job done :D And no, sorry, we haven't been introduced to structures yet, I will take a look at some guides online to get a better understanding. Thanks for pointing that out :) – Hamza A Nov 10 '18 at 17:16
  • What do you mean by "4 values of time"? Are you getting 4 distinct integer counts of seconds for each person ("legs of a race"), and then converting that to minutes/seconds? – HostileFork says dont trust SE Nov 10 '18 at 17:20
  • I've added the other code which should be more descriptive. And yes 4 different times for each leg of a race, you are correct :) I'm just really unsure how to integrate the older code into a format where each number/name in an array can take the 4 times and then do the same thing. – Hamza A Nov 10 '18 at 17:26
  • *"half of what I wrote is gone but that's fine most of it was all mumbo jumbo anyway."* If you feel something important went missing then you can always add it back. But if nothing important went missing then that is worth noticing. :-) Try and keep your questions short and to the point. If you haven't been introduced to structures yet, just use multiple arrays, e.g. `legs1[8]`, `legs2[8]`, etc. and look for instance at [store multiple strings in array](https://stackoverflow.com/q/29568297/), so `names[8][31]`. – HostileFork says dont trust SE Nov 10 '18 at 17:41
  • When writing it up I did notice it was a lot of waffle lol, I just didn't understand how to explain the problem clearly enough so thanks to whoever they editted it for me :) Also thank you for linking me to that, I think that's exactly what I need to store the names and numbers in :) you're a star! Just need to figure out how to assign the times to each name – Hamza A Nov 10 '18 at 17:49
  • @HamzaA That's me. You can see the complete editing history if you click on the link that says things like ["edited 24 minutes ago"](https://stackoverflow.com/posts/53241117/revisions) – HostileFork says dont trust SE Nov 10 '18 at 17:50
  • @HostileFork Ahh alright lol thank you for that :D and thanks for letting me know wasn't aware I could do that. – Hamza A Nov 10 '18 at 17:52

1 Answers1

1

Since you are doing this for an assignment, and haven't been introduced to struct yet, you probably want to avoid using it. But it's how you group related information into a record:

struct Racer {
    char name[31]; 
    int leg1;
    int leg2; 
    int leg3;
    int leg4;
};

int main(int argc, char *argv) {
    struct Racer racers[8];
    int i;
    for (i = 0; i < 8; ++i) {
        scanf("%30s", &racers[i].name);
        scanf("%d", &racers[i].leg1);
        /* ... */
    }
    int totalTime = 0;
    for (i = 0; i < 8; ++i) {
        totalTime = totalTime + racers[i].leg1 + /* ... */
        /* ... */
    }
    /* ... */
}

To avoid using structures at this time, you can just go with parallel arrays. See also "Store multiple strings in array"

int main(int argc, char *argv) {
    char names[8][31];
    int legs1[8];
    int legs2[8];
    int legs3[8];
    int legs4[8];
    int i;
    for (i = 0; i < 8; ++i) {
        scanf("%30s", &names[i]);
        scanf("%d", &legs1[i]);
        /* ... */
    }
    int totalTime = 0;
    for (i = 0; i < 8; ++i) {
        totalTime = totalTime + legs1[i] + /* ... */
        /* ... */
    }
    /* ... */
}

(Sidenote: I'll mention that these two approaches aren't equivalent at runtime because they lay out memory differently. There are reasons why you might choose the separate arrays in some cases...if you're going to be iterating through the arrays individually, it's better to have the data tightly together instead of split out as elements across multiple records.)

  • Thank you so much! This is perfect, I had to edit it to make it totalTime = legs1[i] + legs2[i] ... I'm going to keep working at the code to get the rest of the bits in but thanks again, you solved it perfectly <3 I'd just like to ask what the int argc, char *argv mean, cheers – Hamza A Nov 10 '18 at 23:19
  • @HamzaA We don't write people's assignments for them, so you should have to edit (!) RE: [parameters to main()](https://stackoverflow.com/questions/3734111/what-are-the-arguments-to-main-for) – HostileFork says dont trust SE Nov 11 '18 at 00:16
  • Ahah, I got that from the comment lines :) just felt like mentioning it :P got it all done though :D – Hamza A Nov 11 '18 at 00:18