Question

Programming Question

Exercise #1:

Write a C program that contains the following steps. Read carefully each step as they are not only programming steps but also learning topics that explain how numeric arrays in C work.

  1. Write a while loop to input a series of numbers (either from a file or from the keyboard, but using a file will make for easier debugging) into a one dimensional array of type double named x_arr, declared to be 25 elements in length. Count the elements as you input them. Then in a second loop, a for loop this time, print out all the elements which were input. Make up your own data file for this problem.

  2. Extend your code by writing a for loop to find the largest value in the array. The largest value should go into a variable named xhigh. Your code should then print out xhigh.

  3. Extend your code by writing a for loop to find the smallest value in the array. The smallest value should go into a variable named xlow. Your code should then print out xlow.

  4. Extend your code to produce a second array by using a for loop to iterate through your x_arr array and produce another array x_second_arr where each element in x_second_arr is 3 times the value in the array x_arr. Print out both arrays, with brief labelling so you can tell them apart on your output.

  5. You will next "normalize" the numbers in the array. This takes one array of numbers (x_arr) and puts them into a second "normalized" array named norm_x_arr so that the original values collectively "stretch" or "contract" to fit into a different range. (Both x_arr and norm_x_arr should be declared to be the same length, for example 25.) An example of where this could be used would be to take test results between 0 and 30 ("out of 30") and produce marks between 0 and 100. To do this, you will first input the two new limiting values you choose into two variables named max and min - these will be your upper and lower limits of the new, normalized norm_x_arr (the 0 and 100 of the example). You then copy the code you wrote above to find the largest and the smallest values in x_arr, putting them into xhigh and xlow (these would be the 0 and 30 of the example). Then using the appropriate loop form, run through the elements of x_arr, putting each element in turn into norm_x_arr modified according to the formula

    normxi = min + ((xi - xlow) * (max - min)) / (xhigh - xlow)

    to produce the second array norm_x_arr. In this formula the terms xi and normxi, are referring to single elements of the arrays x_arr and norm_x_arr. The xi will be be turned into the normxi but in the process they are adjusted so that xhigh becomes max, the maximum of all the normalized numbers, and xlow becomes min, the minimum of all the normalized numbers ... with all elements in the xi array being adjusted proprotionally to fall between min and max. As a trivial example, if you had the numbers 2 3 4 and you wanted to "normalize" these to between 0 and 10, you would apply the formula so that 2 3 4 would become 0 5 10. Note that the spacing between the values is maintained proportionally: 2 and 3 and 4 are all 1 apart; 0 and 5 and 10 are all 5 apart. As another example, you could also normalize the same 2 3 4 to be between 14 and 18. 2 would become 14, 3 would become 16, and 4 would become 18. Try this with several different sets of data. Include various orderings of the numbers (not necessarily given in sorted order), put in duplicate values for some of the data elements, and ensure your code works with a mix of positive, zero, and negative values.


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

#include <stdio.h>

#include <stdlib.h>


int main()

{

    //main array

    double x_arr[25];

    //file ointer

    FILE *inFile;

    double temp;

    int index=0;

    //opening the file

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

    //reading the file into the array

    while(!feof(inFile)){

        fscanf(inFile,"%lf", &temp);

        x_arr[index] = temp;

        index++;

    }

    fclose(inFile); //closing the file

    

    //low and high variables

    double xhigh, xlow;

    //second array

    double x_second_arr[25];

    xhigh = x_arr[0];

    xlow = x_arr[0];

    printf("Entered Values\n");

    //displaying the value

    //calculating the second array values

    //calculating the high and low values

    for(int i=0;i<index;i++){

        printf("%.2f\n", x_arr[i]);

        x_second_arr[i] = x_arr[i] * 3;

        if(xhigh < x_arr[i]){

            xhigh = x_arr[i];

        }

        if(xlow > x_arr[i]){

            xlow = x_arr[i];

        }

    }

    //printing the high and low values

    printf("\nHighest Value is: %.2f\n", xhigh);

    printf("Lowest Value is: %.2f\n", xlow);

    

    //printing the second array values

    printf("\nSecond Array Values\n");

    for(int i=0;i<index;i++){

        printf("%.2f\n", x_second_arr[i]);

    }

    

    //normalizing the array

    double max, min;

    //getting the upper and lower limit

    printf("\nEnter upper and lower limit\n");

    scanf("%lf", &max);

    scanf("%lf", &min);

    double norm_x_arr[25];

    //calculating the normalized values

    for(int i=0;i<index;i++){

        norm_x_arr[i] = min + ((x_arr[i]-xlow) * (max-min))/(xhigh-xlow);

    }

    //displaying the normalized values

    printf("\nNormalized Array Values\n");

    for(int i=0;i<index;i++){

        printf("%.2f\n", norm_x_arr[i]);

    }

    return 0;

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Programming Question
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
  • Programming Question

    Exercise #2:Create a data file named water.dat with the following data: 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111. Each number represents the number of millions of gallons of water provided to a major city over the period of one month. The data runs for quite a number of months. You will write a loop to read all the data...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Problem overview. There are a number of ways to normalize, or scale, a set of values....

    Problem overview. There are a number of ways to normalize, or scale, a set of values. One common normalization technique scales the values so that the minimum value goes to 0, the maximum value goes to 1, and the other values are scaled accordingly. Using this normalization technique, the values of the array below are normalized. Original Array Values -1 -2 2 Normalized Array Values 0.25 0.01.0 0.5 The equation that computes the normalized value from a value xe in...

  • In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...

    In this problem you'll get to use one of Java's random number generators, java.util.Random (see the docs for Random). This class generates a random double precision number uniformly distributed in the range [0.0 ... 1.0). That is, the numbers go from 0 to 1, including 0 but excluding 1. It is a common need in Java programming to generate a random integer number that's uniformly distributed in the range of [0 ... n), such as we saw in the dice...

  • Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10...

    Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10 M gals water per day 71-80 91-100 101-110 111-120 121-130 131-140 Create a data file named water.dat with the following data: 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111. Each number represents the number of millions of gallons of water provided to...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • Playing With Arrays The following problems are to give you practice with programming with arrays. Write...

    Playing With Arrays The following problems are to give you practice with programming with arrays. Write a program to: 1.Ask the user for numbers and place them in an array sequencially. 2.Next, print out the values in the array in the order they are stored. 3.Search the array for the postion containing the smallest value. 4.Print out the position where the smallest value was found. 5.Swap the smallest value with the value in the first postition of the array. 6.Print...

  • I keep getting redefinition errors and undeclared identifiers 3. (20 points. Lastname_Lab6 p3.cpp) Write a program...

    I keep getting redefinition errors and undeclared identifiers 3. (20 points. Lastname_Lab6 p3.cpp) Write a program that declares three one dimensional arrays named price, quantity, and amount. Each array should be declared in main ) and should be capable of holding ten double-precision numbers. The numbers that should be stored in price are 10.64, 14.89,15.21,74.21,23.8,61.26,92.37, 12.73, 2.99, 58.98. The numbers should be stored in quantity are 4, 8,17,2,94,61,20,78,55,41. You program should pass these three arrays to a function named extend...

  • C programming Ask the user how many numbers they would like generated. Your program will then...

    C programming Ask the user how many numbers they would like generated. Your program will then generate that many numbers (between 0 and 1000, inclusive) into an array of the same size. Find the smallest and largest numbers in the array, printing out the values and locations of those numbers in the array. Then, find the sum and average of the numbers in the array. Finally, print out the entire array to verify. You MUST use functions where appropriate!

  • C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...

    C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc. Add comments to display assignment //step 1., //step 2. etc. This program is to have no programer created functions. Just do everything in main and make sure you comment each step. Create a program which has: 1. The following arrays created:                 a. an array...

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