This needs to be writen in the C programing language with documantation and the
ID: 652009 • Letter: T
Question
This needs to be writen in the C programing language with documantation and the output must match the sample output.
Write a program that stores the following prices in a global array: 9.92, 6.32, 12.63, 5.95, 10.29. The program should also create two automatic arrays names units and amounts, each capable of storing five double-precision numbers. Using a for loop and a scanf() function call, have the program accept five user-input numbers into the units array. The program should store the product of the corresponding values in the prices and units arrays in the amounts array (for example, amounts[1] = prices[1] * units[1]) and display the price, units and amount in a table. Use pointer references to access the array elements. Sample output:
Explanation / Answer
#include<stdio.h>
#include<conio.h>
float prices[5]={9.92,6.32,12.63,5.95,10.29}; //declare global float array prices
void main(){
float units[5],amounts[5]; //declare float array units and amounts
float total; //declare float varaiable total to sum up and store the amounts
int i=0; //declare loop varaiable
clrscr(); //clear the screen
for(i=0;i<5;i++){ //iterate 5 times
printf("Enter units[%d]:",i); //prompt for input
scanf("%f",(units+i)); //accept the input
}
for(i=0;i<5;i++){ //iterate 5 times
amounts[i]=prices[i]*units[i]; //calculate amount for each item in arrays
}
printf(" Price Units Amount ");
printf("----- ----- ------ ");
total=0; //initialize total to 0
for(i=0;i<5;i++){
printf("$ %.2f %.2f $ %.2f ",*(prices+i),*(units+i),*(amounts+i)); //display the calculated price, unit and amount for each item in array
total+=amounts[i]; //add each amount to total
}
printf(" ");
printf("----- ----- ------ ");
printf("Total: $%.2f",total); //display the total
getch(); //wait at the console after displaying the output till any key is pressed to exit the program
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.