Answer to Question #2680 in C++ for SinaN

Question #2680
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;
}

//int znak;
//int matrix[N][M];
//for(int i=0;i<N;i++)
//for(int j=0;j<M;j++)
//{
//znak=rand()%2;
//matrix[i][j]=rand()%100 * (znak==1?-1:1);
//}
//int sum;
//int negsum;
//for(i=0;i<N;i++)
//{
//sum=0;
//negsum=0;
//for(int j=0;j<M;j++)
//{
//cout<<matrix[i][j]<<'\t';
//if(matrix[i][j]<0) negsum+=matrix[i][j];
//else sum+=matrix[i][j];
//}
//cout<<"negsum="<<negsum<<'\t'<<"possum="<<sum<<endl;
//}
//}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS