Question 1 : There is an array a [20] of float numbers, Enter number in the array. Find the sum of negative number.
Question 2 : Enter 10 integer numbers in the array a [10]. Find the sum of positive numbers.
This question can you help me. plaese
1
Expert's answer
2011-05-23T10:02:24-0400
#include <iostream> #include <time.h> using namespace std;
float a[20]; // The required array of float numbers
// Printing the array void print_array(float a[], const int count);
int main() { srand(time(0));
// Putting the random float numbers into the array int znak; for (int i = 0; i < 20; i++) { & znak = rand()%2; & a[i] = float(rand()) / 357 * (znak == 1 ? -1 : 1); } cout << "The array is: "; print_array(a, 20); cout << endl;
// Finding the sum of negative number: float negsum = 0; for (int i = 0; i < 20; i++) & if (a[i] < 0) & negsum += a[i]; cout << "The sum of negative numbers is: " << negsum << endl;
// Entering 10 random int numbers instead of 10th-19th elements of the array a for (int i = 10; i < 20; i++) { & znak = rand()%2; & a[i] = rand() / 100 * (znak == 1 ? -1 : 1); } cout << "Now array is :"; print_array(a, 20); cout << endl;
// Finding the sum of the positive numbers float sum = 0; for (int i = 0; i < 20; i++) & if (a[i] > 0) & sum += a[i]; cout << "The sum of positive numbers is: " << sum << endl << endl;
return 0; }
// Printing the array void print_array(float a[], const int count) { cout << endl; for (int i = 0; i < count; i++) & cout << a[i] << ", "; cout << endl; }
Comments
Leave a comment