Send
Close Add comments:
(status displays here)
Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website. nbsp; Note: This appears on each machine/browser from which this site is accessed.
Input, process, output using arrays
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. Input, process, output using arrays
We previously covered the following.
C: While loops for input
C: For loops for input
If anything here is not familiar, go back and review where it was previously covered.
The same problem is now solved where arrays are used to separate input, processing, and output.
2. Input, process, output
Arrays can be used to separate input, processing, and output.
Suppose that there are a maximum of 100 data numbers in the input (which does not include any header or trailer values - they are not data numbers).
3. List decision
The data numbers form a logical list. A list is a sequence of items.
There are (at least) two ways to logically represent a list of
n items.
A list from 0 to n-1 .
A list from 1 to n .
Since an array starts at index
0 , a list of
n items from
1 to
n requires an array of
n+1 items.
4. Declarations
Here is one way to represent a list of items from
1 to
100 using on array of
101 items.
The constant (named literal) DATAMAX1 (all upper case, by convention) is set to 100 .
The array dataList1 representing the data list has DATAMAX1+1 elements, each of type int .
Note: In the code examples that follow, there is no check to insure that more than
DATAMAX1 data numbers are added to the list.
5. Input
The input should get the input into the array. Here is an example using a header loop where dataLen1 is known.
6. Input
In a counter or header loop, the data value can be read directly into the array element as follows.
Note: In a trailer or endfile loop, one extra value is needed, so the array should then have DATAMAX1+2 elements instead of DATAMAX1+1 elements.
7. Process
The processing should go through the data list computing the cumulative sum over the data list.
Here, a separate data value variable dataValue1 is used.
8. Process
In some cases, the array element can be access directly in computing the cumulative sum.
9. Header loop
Here is the C code.
10. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
11. Trailer loop
Here is the C code.
12. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
13. End of page