Top Posters
Since Sunday
New Topic  
s.h_math s.h_math
wrote...
Posts: 293
Rep: 3 0
A month ago
Good day,

Could you provide me some insights on this lab if you have any on your end?



Thank you so much.


LAB #7
Purpose:
This lab helps you to learn how to manipulate one dimensional numeric
arrays in C programs.
Before the lab:
1. Review your notes with particular attention to the syntax of
declaring, accessing and reading and writing numeric arrays.
During the lab:
PART I: PROGRAMMING EXERCISES
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.
a. 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.
b. 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.
c. 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.
d. 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.
e. 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.
Exercise #2:
a. 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 into an array of length 50 named
monthly_water_arr. Your code should count the number of data
elements input, putting that value into the variable
num_months and after the entire set of data has been entered,
write another loop controlled by num_months to print out the
values. Provide an appropriate label so you can recognize the
output.
b. Extend your code by writing a loop which will go through the data
in monthly_water_arr and use an if statement in the loop to
print out any values between 71 and 80, inclusive.
c. Modify your code by removing the printing from this latest loop,
replacing it by code to count into a variable named count_0 how
many values, between 71 and 80, inclusive, would have been
printed. When the loop terminates,it will print out the variable
count_0. (You will shortly see why "0" is used for this name.) Try
this with the data.
d. Further modify the loop by summing into another variable
count_1, the number of values between 81 and 90 and after the
loop terminates, print it out along with count_0. (There are none
for this data so you should see 0 being printed for the second
count.)
e. Next, instead of individual summing elements count_0 and
count_1, create an array named count_arr which is 7 elements
in length. Before summing any of the count elements in the array,
use a for loop to ensure that you zero all these elements. Further
modify your loop running over the array monthly_water_arr to
count the elements with values between 71 and 80 into the first
count_arr element (count_arr[0]), and to count the elements
between 81 and 90 into the second count_arr element
(count_arr[1]), and so on. Remember, a counting loop is just a
sum loop which adds one (or uses ++) each time a count is to be
added in. Note from inspecting the data you can see that there
are 7 "bins", grouped in 10's of millions of gallons of water per
bin, into which the water usage amount can fall: 71-80 , 81-90,
... , 131-140 so 7 bins will suffice for count_arr. When the loop
doing the counting is finished, use a for loop to print out the 7
values in count_arr, each on a separate line. These values are
the frequency in months that showed this amount of water usage
per month over the observation period, in groups of 10 million
gals per day. Label each number with the millions of gallons range
it covers. For the given data this would look like
10 M gals water per day
71-80
81-90
91-100
101-110
111-120
121-130
131-140
f. To produce the output with the labelling column on the left is not
terribly trivial, as you will have to change the label for each
iteration of the printing for loop. You can do this with a series of
nested if statements or you can use variables to produce the
labelling numbers, changing the values in those variables with
each for loop iteration.
g. As an additional challenge in the original form of this lab, see if
you can produce the same result but this time only print out nonzero
lines, as in
10 M gals water per day
71-80
91-100
101-110
111-120
121-130
131-140
PART II: DISCOVERY ACTIVITIES
i. Using your text book, or an Internet search (you can ask your TA)
or simply by experimenting with Geany, explain in a few
sentences why arrays and loops are made to work together and
why must we start the array index at 0 instead of 1.
ii. Using your text book, or an Internet search (you can ask your TA)
or simply by experimenting with Geany, explain in a few
sentences how arrays are stored in memory compared to regular
scalar variables.
PART III: LAB REPORT SUBMISSION
1. Submit the .c file for programming exercise 1.
2. Submit the text file containing the answers to the two discovery
questions.
3. Submit on D2L/Brightspace under Lab #7. Submissions are due
at the end of the lab session. You must submit your work before
leaving the lab.
After the lab:
1. Review the steps you took to perform the various operations in
the lab.
Homework:
• On paper (no computer needed), do the following programming
project (write the code by hand as you would on a test or an
exam).
• Show your homework to your lab assistant at the beginning
of next week's lab.
• If you wish, you may try your solution with the computer to
see if you got the correct solution (no need show the computer
version).
Page last modified 01/03/2024 16:11:51Page last modified
01/02/2024 11:40:28
 Attached file 
You must login or register to gain access to this attachment.
Read 65 times
1 Reply
Replies
Answer accepted by topic starter
AnonymousAnonymous
wrote...
A month ago
Sign in or Sign up in seconds to unlock everything for free
1
New Topic      
Explore
Post your homework questions and get free online help from our incredible volunteers
  1220 People Browsing
Related Images
  
 4432
  
 262
  
 776
Your Opinion
Who will win the 2024 president election?
Votes: 3
Closes: November 4

Previous poll results: Who's your favorite biologist?