• 3 Posts
  • 986 Comments
Joined 2 years ago
cake
Cake day: August 21st, 2024

help-circle












  • you can absolutely save history in an array, but depending on how you want to do it, it can get very complex for a beginner.

    the absolutely easiest way to go about it with how you’ve structured the program so far is making global arrays of just numbers, one for C->F conversions and one for the other way round. you also need to keep track of where in each array you are so you don’t go out of bounds. this requires minimal changes.

    float c_to_f[5] ={0,0,0,0,0};
    int c_to_f_length = 5;
    int c_to_f_index = 0;
    

    and update your input handler:

    scanf("%d", &c_value);
    c_to_f[c_to_f_index] = c_value;
    c_to_f_index++;
    if (c_to_f_index >= c_to_f_length) c_to_f_index = 0;
    

    and then when you want to see the list results, you do

    for (int i = 0; i < c_to_f_length; i++) {
        printf("%d\n", c_to_f[i];
    }
    

    this will of course not be in order of insertion, and if you want to show more than just the numbers you need to do some tinkering.

    this is however a great jumping-off point to talk about many different concepts:

    • do you feel like you’re just writing the same thing over and over? look into encapsulation and refactoring your program to minimise repetition.
    • do you want to store more information about each entry? time to learn about structs.
    • do you want to store as many entries as you want, without having to specify? go learn about linked lists.
    • do you want to be able to step back and forward through your history? check out the readline library, and learn how to do imports generally.

    sorry if my formatting is off, i did this on my phone…