In: Computer Science
In a C language program CODE BLOCKS !!!!!!!!
Program that stores the number of computers sold by three vendors
in four different zones. Is required:
Request the sale amount of each seller by zone, the values must
be entered through the keyboard and validate that it does not
accept negative numbers.
Menu that requests the operation to be carried out. In case of
entering an invalid data, send an error message and request the
operation again.
Option to perform another calculation
Note: The code must perform the calculations going through the array, operations assigning the values directly to the array elements are not accepted.
Code in C
#include <stdio.h>
#include <stdlib.h>
void print_menu(){
printf("Enter 1 to get total sales for each vendor.\n");
printf("Enter 2 to get total sales for each zones.\n");
printf("Enter 3 to get total sales across all vendors and zones.\n");
printf("Enter 0 to exit program.\n");
}
int main(){
//2 vendors and 4 zones
const int num_vendors=3, num_zones=4;
int arr[num_vendors][num_zones];
//get sale amount
for(int i=0;i<num_vendors;i++){
for(int j=0;j<num_zones;j++){
int x;
printf("Enter sale amount for vendor %d in zone %d: ", i+1, j+1);
scanf("%d", &x);
while(x<0){
printf("Sales amount cannot be negative!\nEnter sale amount: ");
scanf("%d", &x);
}
arr[i][j]=x;
}
}
int op;
do{
print_menu();
scanf("%d", &op);
while(op<0||op>3){
printf("Invalid option\nPlease input a valid option: ");
scanf("%d", &op);
}
switch(op){
case 1: {
for(int i=0;i<num_vendors;i++){
int total=0;
for(int j=0;j<num_zones;j++){
total+=arr[i][j];
}
printf("Total sales for vendor %d = %d\n", i+1, total);
}
break;
}
case 2: {
for(int i=0;i<num_zones;i++){
int total=0;
for(int j=0;j<num_vendors;j++){
total+=arr[j][i];
}
printf("Total sales for zone %d = %d\n", i+1, total);
}
break;
}
case 3: {
int total=0;
for(int i=0;i<num_vendors;i++){
for(int j=0;j<num_zones;j++){
total+=arr[i][j];
}
}
printf("Total sales = %d\n", total);
break;
}
}
}while(op!=0);
}
Sample Console Input/Output
Enter sale amount for vendor 1 in zone 1: -2
Sales amount cannot be negative!
Enter sale amount: 1
Enter sale amount for vendor 1 in zone 2: 2
Enter sale amount for vendor 1 in zone 3:
3
Enter sale amount for vendor 1 in zone 4: 4
Enter sale amount for vendor 2 in zone 1: 5
Enter sale amount for vendor 2 in zone 2: 3
Enter sale amount for vendor 2 in zone 3: 2
Enter sale amount for vendor 2 in zone 4: 4
Enter sale amount for vendor 3 in zone 1: 2
Enter sale amount for vendor 3 in zone 2: 3
Enter sale amount for vendor 3 in zone 3: 4
Enter sale amount for vendor 3 in zone 4: 1
Enter 1 to get total sales for each vendor.
Enter 2 to get total sales for each zones.
Enter 3 to get total sales across all vendors and zones.
Enter 0 to exit program.
1
Total sales for vendor 1 = 10
Total sales for vendor 2 = 14
Total sales for vendor 3 = 10
Enter 1 to get total sales for each vendor.
Enter 2 to get total sales for each zones.
Enter 3 to get total sales across all vendors and zones.
Enter 0 to exit program.
2
Total sales for zone 1 = 8
Total sales for zone 2 = 8
Total sales for zone 3 = 9
Total sales for zone 4 = 9
Enter 1 to get total sales for each vendor.
Enter 2 to get total sales for each zones.
Enter 3 to get total sales across all vendors and zones.
Enter 0 to exit program.
3
Total sales = 34
Enter 1 to get total sales for each vendor.
Enter 2 to get total sales for each zones.
Enter 3 to get total sales across all vendors and zones.
Enter 0 to exit program.
0
Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.