Question

I need help with the following code... Instructions: This lab builds on the skills from Lab...

I need help with the following code...

Instructions:

This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been read, your program should call various functions to perform processing tasks using the array values.

You will be working in teams of 3, with duties assigned as follows:

Part a.    Together, write and test the function to create a new array of just valid magnitude values

All Team Members:   First add the code needed in your overall large program to read ALL of the values in the data file and save them, one by one, in an array named allMags. Together, write a getValids function, which takes an array of floating point values, and copies only the valid values into a new array also provided as an input parameter. Name your program lastname1_lastname2_lastname3.c, including all of the last names of the team members.

Add this new function getValids below the main function in your program. Don’t forget to add a prototype statement for the function, above main. Question 1: where should the new array containing only valid values be defined? (Hint: Name the new array validMags, but do not create it inside the getValids function.) Question 2: Does each function that processes an array provided as a parameter need to have the length of the array provided as a parameter as well?

Add the code to call the getValids function from main, and test your program to make sure that it works correctly by displaying all of the values in the validMags array after the getValids function returns.

In the next part, the code for the each of the tasks should be completed separately by each team member, tested, and if correct, added to the overall program. Don’t forget to add the functions below main and to define prototypes for each.

THIS IS THE CODE FROM LAB2C:

#include <stdio.h>

int main(void) {

FILE* inFile = NULL; // File pointer

float fileNum; // Data value from file

int value_greater_than_three =0 , invalid_entries = 0 ,i =0;

// Open file

printf("Opening file epquakes.txt \n");

inFile = fopen("epquakes.txt", "r");

if (inFile == NULL) {

printf("Could not open file epquakes.txt \n");

return -1; // -1 indicates error

}

// Print read numbers to output

printf("Reading and printing numbers.\n");

/* while (!feof(inFile)) {

fscanf(inFile, "%f", &fileNum);

printf("num: %f\n", fileNum);

if(fileNum > 3.0)

value_greater_than_three++;

if(fileNum < 0.0)

invalid_entries++;

} */

for(i=0;i<10;i++) {

fscanf(inFile, "%f", &fileNum);

if(fileNum > 3.0)

value_greater_than_three++;

if(fileNum < 0.0)

invalid_entries++;

}

printf("Closing file epquakes.txt \n");

// Done with file, so close it

fclose(inFile);

printf("%d entries are greater than 3 \n",value_greater_than_three);

printf("%d entries are invalid entries \n",invalid_entries);

return 0;

}

THANKS!

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

NOTE: From the code provided I've assumed that the number of entries in the file is 10 and a value less than 0 is invalid. I've added the required code to LAB2C

Answers to Questions:

1: I've defined validMags array in the main() funciton

2: Yes, functions require the size of array as parameter

#include <stdio.h>

//Function Prototype

void getValids(float allMags[], float validMags[], int size);

int main(void) {

FILE* inFile = NULL; // File pointer

//No nedd // float fileNum; // Data value from file

int value_greater_than_three = 0 , invalid_entries = 0 , i = 0;

float allMags[10], validMags[10]; // To store all values

// Open file

printf("Opening file epquakes.txt \n");

inFile = fopen("epquakes.txt", "r");

if (inFile == NULL) {

printf("Could not open file epquakes.txt \n");

return -1; // -1 indicates error

}

// Print read numbers to output

//printf("Reading and printing numbers.\n");

/* while (!feof(inFile)) {

fscanf(inFile, "%f", &fileNum);

printf("num: %f\n", fileNum);

if(fileNum > 3.0)

value_greater_than_three++;

if(fileNum < 0.0)

invalid_entries++;

} */

for (i = 0; i < 10; i++) {

fscanf(inFile, "%f", &allMags[i]);

if (allMags[i] > 3.0)

value_greater_than_three++;

if (allMags[i] < 0.0)

invalid_entries++;

}

// Calling getValids() function

getValids(allMags, validMags, 10);

// Number of valid entries

int valid_entries = 10 - invalid_entries;

// Printing validMags array

printf("Printing valid entries.\n");

for(i = 0; i < valid_entries; i++) {

printf("%f\n", validMags[i]);

}

printf("Closing file epquakes.txt \n");

// Done with file, so close it

fclose(inFile);

printf("%d entries are greater than 3 \n", value_greater_than_three);

printf("%d entries are invalid entries \n", invalid_entries);

return 0;

}

//Function Declarations

void getValids(float allMags[], float validMags[], int size) {

int i,j;

for(i = 0, j = 0; i < size; i++) {

if(allMags[i] > 0.0) {

validMags[j] = allMags[i];

j++;

}

}

}

Code

valid.c epquakes.txt 1 #include«stdio.h> 3 //Function Prototype 4 void getValids (float allMags, float validMags, int size); 6 int main(void) { FILE inFile = NULL; // File pointer //No nedd// float fileNum; // Data value from file int value-greater-than-three- θ , învalid-entries = θ , i = θ; float allMags [10], validMags [10]: // To store all values 10 12 13 14 15 16 1/ Open file 17 18 19 20 21 printf(Opening file epquakes.txt In): inFile-fopen(epquakes.txt, r); if (inFile == NULL) { 23 24 25 26 27 28 29 30 1/ Print read numbers to output 31 32 printf(Could not open file epquakes.txt In) return -1; // -1 indicates error 34 35 36 37 38 //printf( Reading and printing numbers.In) *while (!feof(inFile)) fscanf (inFile, f, &fileNum); printf(num: %f\n, fileNum);epquakes.txt file that I've written for testing

Output

Add a comment
Know the answer?
Add Answer to:
I need help with the following code... Instructions: This lab builds on the skills from Lab...
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
  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • How would I change the following C code to implement the following functions: CODE: #include <stdio.h>...

    How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...

  • 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....

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • 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...

  • 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...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

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