read 10 integer values use them to initialize elements of the array and find the number of positive elements in the array. You are supposed to have 2 functions: for initialization, and for identifying and counting positive elements.
What is this question asking about positive number and counting positive elements? Do i have to crate a negative number?
#include <iostream>
usingnamespace std;
int identifying(int[] , int);
int main ()
{
int size [] = {-1,-2,-3,-4,-5,-6,-7,-8,9,-10,};
int result = identifying(size, 10);
if (result >= 0)
{
cout << "The number are " << size [result] << " are positive and is at " << result <<" element" << endl;
}
else
{
cout <<"The number " << result << " is a negative" << endl;
}
}
int identifying (int find [], int sort)
{
for (int p = 0 ; p< sort; p++)
{
if (sort == find [p])
{
return p;
}
}
return -1;
}
I am pretty sure the question wants you to ask the user to input the 10 values and then have your program output how many values are negative. Did you just skim read the prompt?
Ah, you should have explained that before - I can simplify for you.
1. Ask user for 10 values inside function named "initialize"
2. Inside function named "identify", count how many values are positive
3. Tell the user how many numbers were positive
int initialization (int []);
int identifying (int [],int);
int main()
{
int a[10];
initialization (a);
}
int initialization (int getNum [])
{
for(int s = 0; s < 10 ; s ++)
{
cout <<" Please a enter 10 value : " << endl;
cin >>getNum[s];
}
return 0;
}
int identifying (int findNum [], int num)
{
int positive=0;
int negative=0;
for (int i=0; i < num ; i++)
{
if (findNum [i] >= 0)
{
cout <<"The positive number are " <<positive << endl;
positive ++;
}
else
{
cout <<"The Negative number are " << negative << endl;
negative ++ ;
}
}
return 0;
}
you wrote the function initialize() to get 10 integers from the user but you did not return the values to the identify() function you wrote. Two ways I would do this is to call the function initialize ten time through a for loop and return the value retrieved from the user or pass the array from main by reference and then you will have in main an array with the ten integers. Next call identify function in main and pass the array you filled with the ten integers. Or call the identify function from the initialize function and still pass by reference but accept it as a const integer.
#include<iostream>
#include<string>
usingnamespace std;
int initialization (int []);
int identifying (int []);
constint num = 10;
int main()
{
int a[10];
initialization (a);
}
int initialization (int getNum [])
{
for(int s = 0; s < num ; s ++)
{
cout <<" Please enter a number : # " << (s+1) << " = " << endl;
cin >>getNum[s];
}
identifying (getNum);
}
int identifying (int findNum [])
{
for (int i=0; i < num ; i++)
{
if (findNum[i] >= 0)
{
cout <<"The positive number are " << findNum[i] << endl;
}
else
{
cout <<"The Negative number are " << findNum[i] << endl;
}
}
return 0;
}
this is a quick compiled solution I would have broken down the program into more than the two functions and I would have accepted a const int into identify to ensure the values stored in the array were not changed
Yep your right L B about the line 14 but why not call the identify function from initialization and I missed the return in identify I agree about not having that one as well.
Could you explain your reasoning on the not calling the function in initialization though
It doesn't make sense to do that in a function called "initialize" - if the function were called "run_entire_program" then perhaps it might make more sense.