Design a program that displays the sum of the integer numeric values stored in t
ID: 3674168 • Letter: D
Question
Design a program that displays the sum of the integer numeric values stored in two arrays, in a tabular format. The design defines two arrays of the same size (each array has 10 elements). One array stores odd integer numeric values (the first element of the array (at index 0) stores the value 1, ... etc). Another array stores even integer numeric values (the first element of the array (at index 0) stores the value 2, ..., etc).
Program Requirements:
-The program displays a title (appropriate for the case being solved).
-The program displays on a single line three headings (i.e., labels) – to identify the corresponding odd, even, and sum values.
-The program displays the triplets (i.e., the three numeric values – odd, even, and sum), one triplet per line of output, for a total of 10 output lines.
-The indices of the arrays are zero-based.
-The arrays are dimensioned to contain 10 elements (not 11 eleven elements – NOTE: In Visual Logic if the size of the array is set to 10, the array will have a capacity to store 11 elements).
-The program uses modules.
Expected Submittals:
-The Visual Logic program
-Formal pseudocode, pseudocode must follow the flow of your application, and flowchart (following the book syntax, proper indentation, with variable declarations, and start, stop, and returns as needed, etc.). Use Notepad++ to create the pseudocode (*.txt file)
-Formal flowchart of the application (following the book syntax, proper symbols, must follow the flow of your application, and pseudocode, must be saved as either a *.jpg or *.png file)
Explanation / Answer
#include <stdio.h>
void oneWay(void);
void anotherWay(void);
int main(void) {
printf(" oneWay: ");
oneWay();
printf(" antherWay: ");
anotherWay();
}
/*Array initialized with aggregate */
void oneWay(void) {
int vect[10] = {1,2,3,4,5,6,7,8,9,0};
int i;
for (i=0; i<10; i++){
printf("i = %2d vect[i] = %2d ", i, vect[i]);
}
}
/*Array initialized with loop */
void anotherWay(void) {
int vect[10];
int i;
for (i=0; i<10; i++)
vect[i] = i+1;
for (i=0; i<10; i++)
printf("i = %2d vect[i] = %2d ", i, vect[i]);
}
/* The output of this program is
oneWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 0
antherWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 10
*/
Here is a more complex program that will demonstrate how to read, write and traverse the integer arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[10];
int hmny;
hmny = getIntArray(x, 10, 0);
printf("The array was: ");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is: ");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
int temp = *x;
*x = *y;
*y = temp;
}
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
void printIntArray(int a[], int n){
int i;
for (i=0; i<n; ){
printf(" %d ", a[i++]);
if (i%5==0)
printf(" ");
}
printf(" ");
}
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
int getIntArray(int a[], int nmax, int sentinel)
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full ");
else
a[n++] = temp;
}while (1);
return n;
}
/* It reverse the order of the first n elements of array */
void reverseIntArray(int a[], int n)
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
Copy one array into another
There is no such statement in C language which can directly copy an array into another array. So we have to copy each item separately into another array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
int main()
{
int iMarks[4];
short newMarks[4];
iMarks[0]=78;
iMarks[1]=64;
iMarks[2]=66;
iMarks[3]=74;
for(i=0; i<4; i++)
newMarks[i]=iMarks[i];
for(j=0; j<4; j++)
printf("%d ", newMarks[j]);
return 0;
}
To summarize, arrays are provides a simple mechanism where more than one elements of same type are to be used. We can maintain, manipulate and store multiple elements of same type in one array variable and access them through index.
Multidimensional Arrays
In C Language one can have arrays of any dimensions. To understand the concept
of multidimensional arrays let us consider the following 4 X 5 matrix
Let us assume the name of matrix is x
To access a particular element from the array we have to use two subscripts on for row number and other for column number the notation is of the form X [i] [j] where i stands for row subscripts and j stands for column subscripts. Thus X [0] [0] refers to 10, X [2] [1] refers to 16 and so on In short multidimensional arrays are defined more or less in the same manner as single dimensional arrays, except that for subscripts you require two squire two square brackets. We will restrict our decision to two dimensional arrays.
Below given are some typical two-dimensional array definitions
1
2
float table [50] [50];
char line [24] [40];
The first example defines tables as a floating point array having 50 rows and 50 columns. the number of elements will be 2500 (50 X50).
The second declaration example establishes an array line of type character with 24 rows and 40 columns. The number of elements will be (24 X 40) 1920 consider the following two dimensional array definition int values [3] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10. 11, 12, };
1
2
3
Values [0] [0] = 1 Values [0] [1] = 2 Values [0] [2] = 3 Values [0] [3] = 4
Values [1] [0] = 5 Values [1] [1] = 6 Values [1] [2] = 7 Values [1] [3] = 8
Values [2] [0] = 9 Values [2] [1] = 10 Values [2] [2] = 11 Values [2] [3] = 12
Here the first subscript stands for the row number and second one for column number. First subscript ranges from 0 to 2 and there are altogether 3 rows second one ranges from 0 to 3 and there are altogether 4 columns.
Alternatively the above definition can be defined and initialized as
1
2
3
4
5
6
7
8
9
10
11
int values [3] [4] = {
{
1, 2, 3, 4
}
{
5, 6, 7, 8
}
{
9, 10, 11, 12
}
};
Here the values in first pair of braces are initialized to elements of first row, the values of second pair of inner braces are assigned to second row and so on. Note that outer pair of curly braces is required. If there are two few values within a pair of braces the remaining elements will be assigned as zeros.
Here is a sample program that stores roll numbers and marks obtained by a student side by side in matrix
1
2
3
4
5
6
7
8
9
10
11
12
main ( )
{
int stud [4] [2];
int i, j;
for (i =0; i < =3; i ++)
{
printf (" Enter roll no. and marks");
scanf ("%d%d", &stud [i] [0], &stud [i] [1] );
}
for (i = 0; i < = 3; i ++)
printf (" %d %d", stud [i] [0], stud [i] [1]);
}
#include <stdio.h>
void oneWay(void);
void anotherWay(void);
int main(void) {
printf(" oneWay: ");
oneWay();
printf(" antherWay: ");
anotherWay();
}
/*Array initialized with aggregate */
void oneWay(void) {
int vect[10] = {1,2,3,4,5,6,7,8,9,0};
int i;
for (i=0; i<10; i++){
printf("i = %2d vect[i] = %2d ", i, vect[i]);
}
}
/*Array initialized with loop */
void anotherWay(void) {
int vect[10];
int i;
for (i=0; i<10; i++)
vect[i] = i+1;
for (i=0; i<10; i++)
printf("i = %2d vect[i] = %2d ", i, vect[i]);
}
/* The output of this program is
oneWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 0
antherWay:
i = 0 vect[i] = 1
i = 1 vect[i] = 2
i = 2 vect[i] = 3
i = 3 vect[i] = 4
i = 4 vect[i] = 5
i = 5 vect[i] = 6
i = 6 vect[i] = 7
i = 7 vect[i] = 8
i = 8 vect[i] = 9
i = 9 vect[i] = 10
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.