Partial array output includes garbage

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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 
#include 
#include 

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",
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

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.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.