SENTINEL CONTROL LOOP WHEN DEALING WITH TWO ARRAYS

Joined
May 1, 2023
Messages
7
Reaction score
0
Hello All,
I am having trouble controlling an array, can anybody please help.


Write a program to take two numerical lists of the same length ended by a
sentinel value and store the lists in array x any y, each of which
has 20 elements. Let n be the actual number of data values in each
list. Store the product of corresponing elements x and y in a third
array, z, also of size 20. Display the arrays x, y, and z in a three-column
table. Then compute and display the square root of the sum of the items
in z.

Analysis
inputs: data type double, int
1.1 n = be the actual number of data
1.2 x & y = ends by sentinel value and stores the list into x and y
1.3 z = display the square root of the sum of items in z
1.4 SIZE is 20

Outputs: x, y, z
2.1 x = 20 elements stored
2.2 y = 20 elements stored
2.3 z = the square root of the sum (20 elements)


Contraints:
3.1 length ended by a sentinal value

Formulas
1. none
2. ...

DESIGN
1. Library:
1.1 stdio.h
1.2 math.h

2. main()
2.1 Sub-step
2.2 ...

3. prompt:
3.1 Prompt the user to input data
3.2 Data table for X, Y, and Z
*/

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <math.h>

#define SENTINEL -99
#define SIZE 1


/* Description: Get counted change
Param(s): 4 output parameters
Return Value: none
*/

int main(void)
{
int n = 0; /* data list */
double arrX[SIZE], /* list of numbers */
arrY[SIZE], /* list of numbers */
sum[SIZE], /* the sum of ArX + ArY */
sum_sqr[SIZE]; /* the square root of the sum */


printf("Enter numbers separated by blanks for X & Y\n");
scanf("%lf %lf", &arrX[n], &arrY[n]);

while (arrX[n] != SENTINEL || arrY[n] != SENTINEL) {
sum[n] = arrX[n] + arrY[n];
printf("Enter numbers by blanks for X & Y\n");
scanf("%lf %lf", &arrX[n], &arrY[n]);
}
/*Get Array n, X, Y, and Z data table */
printf("%2s%10s%10s%10s\n", "n", "X", "Y", "Z");
printf("----------------------------------------\n");
for (n = 0; n < SIZE; ++n)
printf("%2d%11.2lf%10.2lf%11.2lf\n", n, arrX[n], arrY[n], sum_sqr[n]);


return(0);
}
 
Joined
Sep 3, 2023
Messages
36
Reaction score
2
Using do/while keeps from having to duplicate your code.

Code:
do{
  //...
} while( arrX[n] != SENTINEL && ++n<SIZE );

for( i=0; i<n; i++){ 
  //...

Add the -lm option to use sqrt function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top