0

Most of my experience is limited to SQL scripting for DBA functions. I am a security specialist and provide help to others on those topics, but I am learning C to aid in those other endeavors. I've been reading books, writing small programs, and expanding the difficulty level as I go. This is the first time I've had to reach out for help. I apologize if this has been asked, but I did search first and didn't find anything.

So far, my programs have always returned only the valid data from partially filled arrays. This particular one is not behaving the same even though I'm using the same for statement I have previously used with success. At this point I must have tunnel vision because I cannot seem to see where this is failing.

If there are fewer than 20 inputs, the printf output displays the remaining values with garbage. It would be greatly appreciated if someone could provide some guidance on what I'm overlooking. Thank you in advance.

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

struct grade
{
    int id;
    int percent;
};

#define maxCount 100

int main()
{
    int *grade;
    struct grade gradeBook[maxCount];
    int count = 0;
    char YN;
    int i;

    for(i = 0; i < maxCount; i++)
        {
            printf("Enter ID:  ");
            scanf("%d", &gradeBook[i].id);

            printf("Enter grade from 0-100: ");
            scanf("%d", &gradeBook[i].percent);
            count++;

            // Prompt to continue, break if done
            printf("Do you want to Continue? (Y/N)");
            scanf(" %c", &YN);
            if(YN == 'n' || YN == 'N')
                {
                break;
                }
        }

void sort(struct grade gradeBook[],int cnt)
{
    int i, j;
    struct grade temp;

    for (i = 0; i < (cnt - 1); i++)
    {
        for (j = (i + 1); j < cnt; j++)
        {
            if(gradeBook[j].id < gradeBook[i].id)
            {
                temp = gradeBook[j];
                gradeBook[j] = gradeBook[i];
                gradeBook[i] = temp;
            }
        }
    }
}

printf("Grades entered and ordered by ID:  \n");
for (i = 0; i < count; i++)
    {
        printf("\nID:%d, Grade: %3d\n", gradeBook[i].id,gradeBook[i].percent);
    }
    return 0;
}

1 Answers1

0

If there are fewer than 20 inputs, the printf output displays the remaining values with garbage

What else did you expect?

If you have fewer than 20 inputs, then the remaining inputs have not been given any value. You say "partial array input" but you literally asked the computer to loop over the entire array.

It's really not clear what else you expected to happen here.

Perhaps loop to count the second time instead.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • As I said, my background is more databases. If it wasn't inserted, it can't be queried. I'm still discovering the differences in how programs store data. I wasn't making the connection to the elements not being empty just because data had not been inserted. So if I'm understanding correctly, at this point I need to discover which elements are populated and loop through outputting just those elements... – Robert Durham Mar 07 '19 at 01:52
  • @RobertDurham You already did that. That's what `count` is. Which book are you using to learn C? – Lightness Races in Orbit Mar 07 '19 at 02:26
  • Programming in C by Stephen Kochan was the one I have been using. – Robert Durham Mar 07 '19 at 02:36
  • Cool, that's [a recommended one](https://stackoverflow.com/a/562377/560648). Good luck! – Lightness Races in Orbit Mar 07 '19 at 02:37
  • I'm still struggling with this. The difficulty is the need to sort on ID. I was using count, but it wasn't sorting by ID. When letting it span the whole array, it sorts correctly, but back fills garbage if not full. I have not been able to understand what I'm missing... – Robert Durham Mar 07 '19 at 04:02
  • At the moment I can't see that you're calling the sort function. – Lightness Races in Orbit Mar 07 '19 at 14:18
  • I wasn't. I realized I broke the output when I was originally working on the sort and lost track of my changes. When I went back to count instead of maxCount, the output worked again, but I am back to the original problem before compounding it. I added the sort code back into the flow, and updated what is here. This was what I had prior to breaking the output. – Robert Durham Mar 08 '19 at 03:34
  • Now you have the `sort` function dangling in the middle of the `main` function. If you indent/format your code properly then you'll be able to spot things like that. – Lightness Races in Orbit Mar 08 '19 at 12:14