Question

Please I need help in C language, I am trying to modify the code per the...

Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you.

Instructions:                 

1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts. This function does not return anything, and you may define your own message. Define this function below main, and add a function prototype statement at top of your program to avoid creating an error flagged by the compiler.

2.      Next, examine your Lab 3 code to identify a computational task that could be successfully removed from main and placed in a separate function instead. Examples of discrete tasks might include:

·       Identifying the minimum value in an array

·       Identifying the maximum value in an array

·       Computing the sum and/or average of values in an array

Define the function to complete the task, placing it below main. You will need to provide a name for this function that matches the task that it performs. Add a function prototype statement for this function at the top of your program.   Add the statement to main that will call this function and handle what is returned. Comment out the lines no longer needed in main.

3.      Test your program using the same data file as used for Lab 3. Your program should produce the identical results as obtained with your program from Lab 3 (with the addition of the greeting/introduction). Capture a screenshot and place it in a Lab Report.   

data.txt

Sean White 96 98 88 99 98 100
Chloe Kim 92 99 100 98 99 97
Ben Ferguson 90 95 91 85 94 88
St. Bernard 99 97 100 98 99 100

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function prototype statement defined
float computeAdjAvg(float adj_avg[], float adj_sum[], int i, int scores[][6]);
void printMsg();
int main()
{

    /*Declared variables, adjusted 1D & 2D arrays to capture data of 4 snowboarders*/
    char firstName[4][15];
    char lastName[4][20];
    int scores[4][6];
    int i,j;
    int sum[4];
    float adj_sum[4];
    float adj_avg[4];
    printMsg();
    FILE *fp; /* Statement to open data file associated with fp in read mode */
    fp=fopen("data.txt", "r"); /* Statement to open data file associated with fp in read mode */

    if (fp == NULL) /* Statement to check if open was successful */
    {
        printf("Program cannot open the file\n");
        return -1;   /* Signal to indicate a problem; hence no need to continue else statement below*/
    }
    /*else*/   /*Optional "else" inserted, not required, but helps mark place*/
    i = 0;
    while (fscanf(fp,"%s %s",&firstName[i],&lastName[i])== 2 && i < 4) /*Adjusted the inequality constrain of compound test to based on "data.txt" provided*/
    {
             int maxScore,minScore,indexMax;
            /*Prompt and read in data for each competitor */
            printf("Please enter First Name, Last Name and 6 scores(2 spaces between input data)for each competitor.\n" );
            printf("The name entered is: %s %s\n", firstName[i], lastName[i]);
            for(j=0; j<6; j++)
            {
                fscanf(fp,"%d", &scores[i][j]);
                printf("The score is: %d\n", scores[i][j]);
            }
            /*A loop construct to find the maximum score*/
            maxScore=scores[i][0];
            for(j=0; j<6; j++)
            {
                if(scores[i][j] > maxScore)
                maxScore= scores[i][j];
            }
            printf("The highest score is: %d\n",maxScore);
            /*A loop construct to find the minimum score*/
            minScore = scores[i][0];
            for(j=0; j<6; j++)
            {
                if(scores[i][j] < minScore)
                minScore = scores[i][j] ;
            }
            printf("The lowest score is: %d\n",minScore);
            /*A loop construct to find sum of the 6 scores*/
            sum[i] =0;
            for(j=0; j<6; j++)
            {
                sum[i]= sum[i] +scores[i][j];
            }
            printf("The sum is: %d\n", sum[i]);
            /*A construct to subtract max and min from sum*/
            adj_sum[i]= sum[i]-maxScore-minScore;
            printf("The adjusted sum is: %f\n", adj_sum[i]);
            /*A construct to compute the average of adjusted sum*/
            int k = 4;
            adj_avg[i]= adj_sum[i]/k;
            printf("The adjusted average is: %f\n", adj_avg[i]);
        }
        /*Construct to find index of the highest adjusted average*/
        int q = 0;
        int r;
        float max = adj_avg[q];
        for (r = 0; r < 4; r++)
        {
            if (adj_avg[r] > max)
            {
                max = adj_avg[r];
                q = r;
            }
        }
        printf("The highest adjusted average score is the winner!\n");
        printf("The winner is: %s %s with an adjusted average score of %f\n", firstName[q], lastName[q],max);
        i++;

    fclose(fp);
    return(0);
}
//Test new function
float computeAdjAvg(float adj_avg[], float adj_sum[], int i, int scores[][6])
{
int maxScore = getMaxScore(scores, i);
int minScore = getMinScore(scores, i);
int sum = getSumOfScores(scores, i);
float adj_sum[i] = float(sum - maxScore - minScore);
int k=4;
adj_avg[i] = adj_sum[i]/float(k);
printf("The adjusted average is: %f\n", adj_avg[i]);
return adj_avg[i];

}
void printMsg(){
printf("Welcome to Functions Practice!\n");
printf("Get busy)!!!\n");
}
int getMinScore(int scores[][6], int i){
minScore = scores[i][0];
int j;
for(j=0; j<6; j++)
{
if(scores[i][j] < minScore)
minScore = scores[i][j] ;
}
printf("The lowest score is: %d\n",minScore);
return minScore;
}


int getMaxScore(int scores[][6], int i){
int j;
int maxScore = scores[i][0];
for(j=0;j<6;j++){
if(scores[i][j] > maxScore)
maxScore= scores[i][j];
}
printf("The highest score is: %d\n", maxScore);
return maxScore;
}

int getSumOfScores(int scores[][6], int i){
int sum[i] = 0;
int j;
for(j=0; j<6; j++){
sum[i]= sum[i] +scores[i][j];
}
printf("The sum is: %d\n", sum[i]);
return sum[i];
}

int getIdxOfHighestAdjAvg(float adj_avg[]){
int q = 0;
int r;
float max = adj_avg[q];
for (r = 0; r < 4; r++){
if (adj_avg[r] > max){
max = adj_avg[r];
q = r;
}
return q;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function prototype statement defined
float computeAdjAvg(float adj_avg[], float adj_sum[], int i, int scores[][6]);
void printMsg();
int main()
{

/*Declared variables, adjusted 1D & 2D arrays to capture data of 4 snowboarders*/
char firstName[4][15];
char lastName[4][20];
int scores[4][6];
int i,j;
int sum[4];
float adj_sum[4];
float adj_avg[4];
printMsg();
FILE *fp; /* Statement to open data file associated with fp in read mode */
fp=fopen("data.txt", "r"); /* Statement to open data file associated with fp in read mode */

if (fp == NULL) /* Statement to check if open was successful */
{
printf("Program cannot open the file\n");
return -1; /* Signal to indicate a problem; hence no need to continue else statement below*/
}
/*else*/ /*Optional "else" inserted, not required, but helps mark place*/
i = 0;
while (fscanf(fp,"%s %s",&firstName[i],&lastName[i])== 2 && i < 4) /*Adjusted the inequality constrain of compound test to based on "data.txt" provided*/
{
int maxScore,minScore,indexMax;
/*Prompt and read in data for each competitor */
printf("Please enter First Name, Last Name and 6 scores(2 spaces between input data)for each competitor.\n" );
printf("The name entered is: %s %s\n", firstName[i], lastName[i]);
for(j=0; j<6; j++)
{
fscanf(fp,"%d", &scores[i][j]);
printf("The score is: %d\n", scores[i][j]);
}
/*A loop construct to find the maximum score*/
maxScore=scores[i][0];
for(j=0; j<6; j++)
{
if(scores[i][j] > maxScore)
maxScore= scores[i][j];
}
printf("The highest score is: %d\n",maxScore);
/*A loop construct to find the minimum score*/
minScore = scores[i][0];
for(j=0; j<6; j++)
{
if(scores[i][j] < minScore)
minScore = scores[i][j] ;
}
printf("The lowest score is: %d\n",minScore);
/*A loop construct to find sum of the 6 scores*/
sum[i] =0;
for(j=0; j<6; j++)
{
sum[i]= sum[i] +scores[i][j];
}
printf("The sum is: %d\n", sum[i]);
/*A construct to subtract max and min from sum*/
adj_sum[i]= sum[i]-maxScore-minScore;
printf("The adjusted sum is: %f\n", adj_sum[i]);
/*A construct to compute the average of adjusted sum*/
int k = 4;
adj_avg[i]= adj_sum[i]/k;
printf("The adjusted average is: %f\n", adj_avg[i]);
}
/*Construct to find index of the highest adjusted average*/
int q = 0;
int r;
float max = adj_avg[q];
for (r = 0; r < 4; r++)
{
if (adj_avg[r] > max)
{
max = adj_avg[r];
q = r;
}
}
printf("The highest adjusted average score is the winner!\n");
printf("The winner is: %s %s with an adjusted average score of %f\n", firstName[q], lastName[q],max);
i++;

fclose(fp);
return(0);
}
//Test new function
float computeAdjAvg(float adj_avg[], float adj_sum[], int i, int scores[][6])
{
int maxScore = getMaxScore(scores, i);
int minScore = getMinScore(scores, i);
int sum = getSumOfScores(scores, i);
adj_sum[i] = 1.0*(sum - maxScore - minScore);
int k=4;
adj_avg[i] = adj_sum[i]/(k*1.0);
printf("The adjusted average is: %f\n", adj_avg[i]);
return adj_avg[i];

}
void printMsg(){
printf("Welcome to Functions Practice!\n");
printf("Get busy)!!!\n");
}
int getMinScore(int scores[][6], int i){
float minScore = scores[i][0];
int j;
for(j=0; j<6; j++)
{
if(scores[i][j] < minScore)
minScore = scores[i][j] ;
}
printf("The lowest score is: %d\n",minScore);
return minScore;
}


int getMaxScore(int scores[][6], int i){
int j;
int maxScore = scores[i][0];
for(j=0;j<6;j++){
if(scores[i][j] > maxScore)
maxScore= scores[i][j];
}
printf("The highest score is: %d\n", maxScore);
return maxScore;
}

int getSumOfScores(int scores[][6], int i){
int sum[i];
int j;
for(j=0; j<6; j++)
sum[j]=0;
for(j=0; j<6; j++){
sum[i]= sum[i] +scores[i][j];
}
printf("The sum is: %d\n", sum[i]);
return sum[i];
}

int getIdxOfHighestAdjAvg(float adj_avg[])
{
int q = 0;
int r;
float max = adj_avg[q];
for (r = 0; r < 4; r++){
if (adj_avg[r] > max)
{
max = adj_avg[r];
q = r;
}
return q;
}
}

CAUsers CS_Nishtha\ Deskt Ơ no-str.c-Executing-Dev-C++ 5.11 exe e lcone to Functions Practice et busy>!!! Please enter First

Add a comment
Know the answer?
Add Answer to:
Please I need help in C language, I am trying to modify the code per the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • Please help in C: with the following code, i am getting a random 127 printed in...

    Please help in C: with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 not sure why...please...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

  • I need some with this program, please look at the changes I need closely. Its running...

    I need some with this program, please look at the changes I need closely. Its running correctly, but I need to do a few adjustments here is the list of changes I need: 1. All of the integers on a single line, sorted in ascending order. 2. The median value of the sorted array on a single line 3. The average of the sorted array on a single line Here is the program: #include<stdio.h> #include<stdlib.h> /* This places the Adds...

  • I am trying to figure out why my C code is outputting "exited, segmentation fault". The...

    I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...

  • Convert this C program to Js (Java script) from Visual Studio Code #include<stdio.h> int main(){ char...

    Convert this C program to Js (Java script) from Visual Studio Code #include<stdio.h> int main(){ char firstName[100]; char lastName[100]; printf("Enter Your Full Name: \n"); scanf("%s %s", firstName, lastName); printf("First Name: %s\n", firstName); printf("Last Name: %s\n", lastName); return 0; }

  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • Trying to debug a C program given to me. This is what I was given... //...

    Trying to debug a C program given to me. This is what I was given... // Program to read numeric elements (including decimals) into a 3X3 matrix and display them #include<stdio.h> int main(void) {    int size = 3, Matrix[size][size];    printf("Enter 9 elements of the matrix:\n") for (int i = 0, i <=size, i++}        for (int j = 0, j <= size, i++)            scan("%c", Matrix1[2][2]);    diplay(Matrix) float display(int Matrix1[][], int size) (   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT