1. Write a program that reads in a list of integer values one per line and outputs their sum as well as the numbers read with each number annotated to say what percentage it contributes to the sum. Your programs will ask the user how many integers there will be, create an array of that length, and then fill the array with the integers input.
#include <iostream>
using namespace std;
int main(){
int *arr, size = 0, sum = 0;
cout<<"Input number of integers: ";
cin>>size;
arr = new int[size];
for(int i = 0; i < size; i++) {
cin>>arr[i];
sum += arr[i];
}
cout<<"\nNumber\tPercentage\n";
for(int i = 0; i < size; i++){
cout<<arr[i]<<"\t"<<(float)arr[i] / sum * 100<<"%"<<endl;
}
return 0;
}
Comments
Leave a comment