Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:38:36+00:00 2026-06-15T19:38:36+00:00

Use a fixed length array of maximum size 50. In case the input file

  • 0

Use a fixed length array of maximum size 50. In case the input file contains more than 50 students, print a message such as “The file contains more than 50 students!” and terminate the program.

i do not know how to add this. i’ve tried many ways using if/else statements but always getting segmentation 11 problems. please help

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

#define MAX_SIZE 50

// Function declarations
void printInfo();
int readresults(FILE*,int*, int*, int*);
void sort(float avgScore[], int stuID[], int totalStu);
float calcAvg (int score1, int score2);
void getInfo(float avgScore[], int totalStu, float* min, float* max, float* avg);
void writeSortedResults (FILE* AG_Sorted, float avgScore[], int stuID[], int totalStu);
void printdone();

int main (void)
{
// Local Declarations
    FILE* AG_Midterm;
    FILE* AG_Sorted;

    int score1[MAX_SIZE];
    int score2[MAX_SIZE];
    int stuID[MAX_SIZE];
    int i = 0;
    int totalStu = 0;

    float avgScore[MAX_SIZE];
    float avg;
    float min;
    float max;

// Statements
    printInfo();

    AG_Midterm = fopen("/Users/r3spectak/Desktop/AG_Midterm.txt", "r");
    if(AG_Midterm == NULL)
    {
      printf("\aError opening Results File\n");

      return 100;

    } // if open input

    if(!(AG_Sorted = fopen ("/Users/r3spectak/Desktop/AG_Sorted.txt","w")))
    {
      printf("\aError opening Average Results file\n");
      return 102;
    }// if open input
    i = 0;


    while(readresults(AG_Midterm, &stuID[i], &score1[i], &score2[i]))//Kexy
        {
          avgScore[i] = calcAvg(score1[i], score2[i]);
            i++;//Kexy
            totalStu++;//Kexy


        } //while

    sort(avgScore, stuID, totalStu);//Kexy

    getInfo( avgScore, totalStu, &min, &max, &avg );
    printf( "\nHighest Average Score: %.2f\nLowest Average Score: %.2f\nClass Average Score : %.2f\n", max, min, avg );
    printf("Total Students: %d\n", totalStu);

    writeSortedResults(AG_Sorted, avgScore, stuID, totalStu);

    printf("Total Students: %d\n", totalStu);

    fclose(AG_Midterm);
    fclose(AG_Sorted);


    printdone();
    return 0;

} // main


/*==================printInfo==================================
    Reads data from Midterm file
    Pre : Nothing
    Post: Prints introduction message
*/

void printInfo()
{
// Statements
    printf("Begin Calculation of Scores\n");
    return ;

} // printInfo


/*===================readResults==================================
    Reads data from AG_Midterm file
    Pre : AG_Midterm is an open file.
          stuid, score1 , score2
    Post: reads score1 and score2
          if data read -- returns 1
          if EOF or error--returns 0
*/

int readresults(FILE* AG_Midterm, int * stuID, int *score1, int *score2)
{
// Local Variables
    int i;
    int items;
    int ioCheck;

// Statements
    ioCheck = fscanf(AG_Midterm, "%d%d%d",stuID, score1, score2);

    if(ioCheck == EOF)

    {
        return 0;
    }

    else if(ioCheck != 3)

    {
        printf("Error reading file...\n");
        return 0;
    }

    else

        return 1;

} //readresults

/*===================sort===================================
    Sorts by selecting the largest Average score in unsorted
    portion of array and exchanging it with element at the
    beginning of the unsorted list.=
    Pre : list must contain atleast one item
    Post: list rearranged largest to smallest

*/

void sort(float avgScore[], int stuID[], int totalStu)
{
// Local Declarations
    int i = 0;
    int j = 0;
    int largest;
    int tempID;

    float temp;

    for(i = 0; i < totalStu; i++)
    {
        largest = i;
        for(j = i + 1; j < totalStu; j++)
        {
            if(avgScore[j] > avgScore[largest])
                largest = j;

        }

        temp = avgScore[i];
        tempID = stuID[i];

        avgScore[i] = avgScore[largest];
        stuID[i] = stuID[largest];

        avgScore[largest] = temp;
        stuID[largest] = tempID;
   }
} // sort


/*===================calcAvg==================================
    Determines the Average of the two midterm scores
    Pre : score1, score2
    Post: avgScore copied to addresses

*/
float calcAvg (int score1, int score2)

{
    return (score1 + score2)/2.0;
} // calcAvg


/*===================getInfo==================================
    Determines the Class Average,Lowest Average
    and Highest Average scores
    Pre : avgScore , totalStu
    Post: avg , min , max

*/
void getInfo(float avgScore[], int totalStu, float* min, float* max, float* avg)
{
   int i;
   float sum = *min = *max = avgScore[0];

   for( i = 1; i <totalStu; ++i )
   {
        sum += avgScore[i]; /* sum = sum + dAry[i] */
        if( avgScore[i] < *min ) *min = avgScore[i]; /* update ... if applies */
        if( avgScore[i] > *max ) *max = avgScore[i];
    }
        *avg = sum/totalStu;
    return;
}

/*===================writeSortedResults==================================
    Writes Average Scores after Sorting
    Pre : AG_Sorted is an open file
          avgScore
    Post: Data written to file
*/
void writeSortedResults (FILE* AG_Sorted, float avgScore[], int stuID[], int totalStu)
{
// Local Definitions
    int i;
    int line = 0;

// Statements
    for(i = 0; i < totalStu; i++)
    {
        fprintf(AG_Sorted, "%d %.2f     ", stuID[i], avgScore[i]);
        line++;

        if(line == 8)
        {
            fprintf(AG_Sorted, "\n");
            line = 0;
        }

    }

    printf("\nSuccessfully write the sorted data to the new file\n");
    return;


} // writeSortedResults

/*==================printDone==================================
    Reads data from Midterm Score File
    Pre : Nothing
    Post: Prints end message
*/
void printdone()
{
// Statements
    printf("End of Finalizing Scores.Thank you.\n");
    return;

} // printDone
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:38:37+00:00Added an answer on June 15, 2026 at 7:38 pm

    If I understand your intent, you’re not just interested in avoiding an overrun like everyone seems to be fixing; you’re trying to abort outright if the input file would overrun, but without actually letting it overrun and therefore seg-fault your process. Checking for feof() won’t do you any good since you may have a linefeed, some whitespace, almost anything in there that is not a regular record entry keeping feof() from being true.

    To address your desired outcome (abort on too large a file) you can read data into local vars, and so long as you’re under the size limit, drop them into your arrays then; not during the read. Otherwise abort.

    int id, s1, s2;
    while(readresults(AG_Midterm, &id, &s1, &s2))//Kexy
    {
        // we *know* we have a valid record. make sure it fits
        //  before dropping it in, otherwise proclaim the file too
        // large and abort the whole thing.
        if (i == MAX_SIZE)
        {
            printf("Too many scores. Aborting.\n");
            exit(EXIT_FAILURE);
        }
    
        stuID[i] = id;
        score1[i] = s1;
        score2[i] = s2;
    
        avgScore[i] = calcAvg(score1[i], score2[i]);
        i++;//Kexy
        totalStu++;//Kexy
    
    } //while
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any method that I can use that returns a fixed length array
I have a fixed-length data file need to persistence into database. I use a
I have fixed length char array used to store my decoded data. Once finish
First off I use this code to make the navigation bar always stay fixed;
Context: I use an ArrayCollection object as follows: 1) Number of elements is fixed.
Use Case Show a photo uploaded by the user in a square box with
use C#,want to upload excel file on google doc. bellow syntax use to upload
use strict; use warnings; use Time::HiRes qw(sleep); use Test::WWW::Selenium; use Test::More no_plan; use Test::Exception;
I have an array of bytes (any length), and I want to encode this
I construct an array, which I am going to use later to compare some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.