Question

Write a C language program that will prompt for 12 different resistor values, entered one at...

Write a C language program that will prompt for 12 different resistor values, entered one at a time via the keyboard, expressed in Ohms. Valid values are 0.01 Ohm up to 10,000,000,000 Ohms. Write each entry to a text file on the disk thus: Resistor 1 Ohmic value = 2200. Update the resistor number for each of the 12 entries. Do not repeat any resistance value.

Write a second C language program to read the 12 entries from the text file. Then convert each resistance value from character data to a number. Find and print to the screen the minimum and maximum resistance values entered. Compute and print to the screen the sum of all the resistance values and the average of the 12 values.

First Program is:

#include
#include
int main(void) {
   FILE* fp;
   float R[12]; //Array of values to be entered
   int k;
   char filename[] = "resistors.txt"; //Stored in directory with programs
   if ((fp = fopen(filename, "w")) == NULL) { //Open for writing
       fprintf(stderr, "\nError opening %s", filename);
       exit(1);
   }
   for (k = 0; k < 12; ++k) { //Loop reads in 12 resistor values
       printf("\nEnter resistor %2i value in Ohms: ", k + 1);
       fflush(stdin);
       scanf("%f", &R[k]);
       fprintf(fp, "Resistor %2d = %.3f\n", k + 1, R[k]);
   }
   fclose(fp); //Close file
   return(0);
}

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

SECOND PROGRAM:

C CODE:

#include<stdio.h>
#include<stdlib.h>
int main(void) {
    FILE* fp;
    float R[12]; //Array of values to be entered
    int k;
    char filename[] = "resistors.txt"; //Stored in directory with programs
    if ((fp = fopen(filename, "r")) == NULL) { //Open for reading
        fprintf(stderr, "\nError opening %s", filename);
        exit(1);
    }
    char word[50];// To read unnecessary text from the file
    for (k = 0; k < 12; ++k) { //Loop reads in 12 resistor values
        fscanf(fp, "%s %s %s %f", word, word, word, &R[k]);
    }
    float min = R[0], max = R[0], sum = R[0], avg;
    for(k = 1; k < 12; ++k){
        if(max < R[k])
            max = R[k];
        if(min > R[k])
            min = R[k];
        sum += R[k];
    }
    avg = sum/12;
    printf("Minimum resistance value: %.3f ohm\n", min);
    printf("Maximum resistance value: %.3f ohm\n", max);
    printf("Sum of all resistance values: %.3f ohm\n", sum);
    printf("Average of all resistance values: %.3f ohm\n", avg);
    fclose(fp); //Close file
    return(0);
}

INPUT FILE USED:

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Write a C language program that will prompt for 12 different resistor values, entered one at...
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
  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Write a C Program that uses file handling operations of C language to copy the contents...

    Write a C Program that uses file handling operations of C language to copy the contents of a file called “original.dat” to another file called “copy2.dat”. Here, you will read and write from/to the files line by line with the help of fgets() and fputs() functions. Sample file “original.dat” has been provided. Please note that the new file “copy2.dat” should have exactly same contents as in “original.dat”. #include <stdio.h> int main() { FILE *fptr1, *fptr2; if((fptr1=fopen("original.dat","r"))==NULL) printf("\n Error opening File");...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • in c++ language, using quincy compiler Write a program that will read in 12 characters from...

    in c++ language, using quincy compiler Write a program that will read in 12 characters from the keyboard between a – z in lowercase. These letters will be inserted into a queue (imported from the Queue.h header file). When the 12th character has been entered, the queue will serve the characters one at a time to a character variable, which will push the values, one at a time, into a stack (using code imported from the Stack.h header file) and...

  • Need this in c programming

    Question:Many files on our computers, such as executables and many music and video files, are binary files (in contrast to text files). The bytes in these files must be interpreted in ways that depend on the file format. In this exercise, we write a program data-extract to extract integers from a file and save them to an output file. The format of the binary files in this exercise is very simple. The file stores n integers (of type int). Each...

  • In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a...

    In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

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