Question

You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questio...

You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questions. The text file has 10 numbers.

1. You will scan the 10 values located in “sensor.txt” to an array of variable type double, using a loop of your choice.

2. You will create a user defined function that will take your array as an input and find the average value. (Ex. arr[] = {15, 12, 13, 10}, the sum will be 50 then divide by the number of values which gives you 12.5).

3. You will save the resulting average to a file called “results.txt” and also output it to the screen.

4. You will create a conditional statement that only outputs values from 20 to 50 to a second output file called “sensor2.txt”.

do it on c pls

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

// 2
double avergae(double values[], int size) {
    int i;
    double total = 0;
    for (i = 0; i < size; ++i) {
        total += values[i];
    }
    return total / size;
}

int main() {
    // 1. You will scan the 10 values located in “sensor.txt” to an array of variable type double, using a loop of your choice.
    int i;
    double arr[10];
    FILE *fp = fopen("sensor.txt", "r");
    if (!fp) {
        printf("sensor.txt does not exists. Exiting!\n");
    }
    for (i = 0; i < 10; ++i) {
        fscanf(fp, "%lf", arr + i);
    }
    fclose(fp);
    // 2. You will create a user defined function that will take your array as an input and find the average value. (Ex. arr[] = {15, 12, 13, 10},
    // the sum will be 50 then divide by the number of values which gives you 12.5).
    printf("Average = %lf\n", avergae(arr, 10));
    // 3. You will save the resulting average to a file called “results.txt” and also output it to the screen.
    FILE *fw = fopen("results.txt", "w");
    fprintf(fw, "%lf\n", avergae(arr, 10));
    fclose(fw);
    // 4. You will create a conditional statement that only outputs values from 20 to 50 to a second output file called “sensor2.txt”.
    fw = fopen("sensor2.txt", "w");
    for (i = 0; i < 10; ++i) {
        if (arr[i] >= 20 && arr[i] <= 50) {
            fprintf(fw, "%lf\n", arr[i]);
        }
    }
    fclose(fw);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
You are provided with an input file called "sensor.txt" and a "code skeleton" of the exam questio...
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
  • Do it on (c) pls..... 1. Assume you have an input file with 6 integer values....

    Do it on (c) pls..... 1. Assume you have an input file with 6 integer values. Scan these values into an appropriate array. 2. Create a user defined function that takes your array as an input and order your array from lowest to highest. Ex. If you have an array called num, then num[0] should contain the smallest value while num[5] will contain the largest value. 3. Output the values between 50-75 into an output file called “results.txt” 4. Any...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • Need code written for a java eclipse program that will follow the skeleton code. Exams and...

    Need code written for a java eclipse program that will follow the skeleton code. Exams and assignments are weighted You will design a Java grade calculator for this assignment. A user should be able to calculate her/his letter grade in COMS/MIS 207 by inputting their scores obtained on worksheets, assignments and exams into the program. A skeleton code named GradeCompute.java containing the main method and stubs for a few other methods, is provided to you. You must not modify/make changes...

  • ****Using C language***** Assume you have an input file with 10 characters. Scan these values in...

    ****Using C language***** Assume you have an input file with 10 characters. Scan these values into an appropriate array. Convert all the characters into Upper Case, then find their ascii value (like done in the homework) and save to a new array, order these values from highest to lowest. Save to file called “upper.txt” Convert all the characters into Lower Case, then find their ascii value (like done in the homework) and save to a new array, order these values...

  • have to create five different functions above and call it in the main fucntion. Project Exam...

    have to create five different functions above and call it in the main fucntion. Project Exam Statistics A CIS 22A class has two midterm exams with a score between 0 and 100 each. Fractional scores, such as 88.3 are not allowed. The students' ids and midterm exam scores are stored in a text file as shown below // id exam1 exam2 DH232 89 92 Write a program that reads data from an input file named exams.txt, calculates the average of...

  • The input file should be called 'data.txt' and should be created according to the highlighted instructions...

    The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the...

  • Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions....

    Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions. Each function will take an array of integers as a parameter and sort those integers in increasing order. One will use insertion sort, and the other will use selection sort (described below). Simple functions will suffice here; do not create any classes. Your insertion sort function should be called insertion_sort(arr). Your selection sort function should be called selection_sort(arr). Selection sort must use a while...

  • Use C++ and must be abke to compile on Visual Studios 9:27 ToDo Final Exam Grade...

    Use C++ and must be abke to compile on Visual Studios 9:27 ToDo Final Exam Grade Detail You need to implement C++ code that will read data from a file and process it as outlined below and write the processed data to an output file. You can refer to the file input.txt for how the data is made available to you and output.txt for how to write out the processed data. Your code will be tested using an input file...

  • 2. In a file called passfail.py, write a Python program to solve the following problem: Given...

    2. In a file called passfail.py, write a Python program to solve the following problem: Given a file students.txt with the following information for a stu dent: student number, average quiz mark, average assignment mark, midterm exam mark, and final exam mark (where each mark is recorded out of 100), de termine if the student has passed or failed based on the following information A student will pass the course if they have a passing mark (50% or higher) for...

  • python question 2. In a file called passfail.py, write a Python program to solve the following...

    python question 2. In a file called passfail.py, write a Python program to solve the following problem: Given a file students.txt with the following information for a stu dent: student number, average quiz mark, average assignment mark, midterm exam mark, and final exam mark (where each mark is recorded out of 100), de termine if the student has passed or failed based on the following information: • A student will pass the course if they have a passing mark (50%...

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