Write a program that first reads at most 30 numbers of type double from the user
ID: 3935393 • Letter: W
Question
Write a program that first reads at most 30 numbers of type double from the user (the user will type character quit when finished), and stores these values in an array in the same order that they were entered by the user. Your program should then call the function reverse array with this array as an argument, and finally displays the new contents of the array to the screen, 5 values per line. Sample Input/Output: (the user's input is shown in bold) Please enter at most 30 numbers; type quit when finished 1396 470 -9 -1 9180-5011 02 -275 13 216 15-7 quit The array in reverse order (5 values per line) is -715 216 13-275 2 0-5011 9180-1 -9 470 1396Explanation / Answer
Please follow the code and comments for description :
CODE :
#include <stdio.h> // required header files
#include <unistd.h>
#include <stdlib.h>
void printReverse(double *d, double *newArray, int n) { // function to print the reverse array
int i;
int j; // local variables
int count = 0;
printf("The array in reverse Order (5 values per line) is : ");
for (i = 0, j = n - 1; i < n; i++, j--) { // iterate over the nodes
newArray[i] = d[j]; // assign data to new array
if (abs(newArray[i]) != 0) { // check for the value
count++; // increment the count
printf("%0.2lf ", newArray[i]); // print the data
if (count % 5 == 0) { // check for new line count
printf(" ");
}
} else {
continue;
}
}
}
int main() { // driver method
double array[30]; // array to store the data
printf("Enter array elements: "); // message
for (int i = 0; i < 30; i++) { // iterate to get the data
if (scanf("%lf", &array[i]) == 'q') {
break;
}
}
size_t n = sizeof (array) / sizeof (double); // get the size of the data
double newArr[n];
printReverse(array, newArr, n); // call the function
}
OUTPUT :
Enter array elements:
-132
-5.6
8
9
9.3
6.4
7.8
20
-96.4
-6845.1
quit
The array in reverse Order (5 values per line) is :
-6845.10 -96.40 20.00 7.80 6.40
9.30 9.00 8.00 -5.60 -132.00
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.