Question about this!

Pages: 12
I am kind dont understand what is this ask!

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?
Try what you think it is asking for and post what you have when you get stuck, then we can help further.

I think you just need to read more carefully - the meaning is quite clear.
This is what the question mean?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream> 
using namespace 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;
}


but it only search the first number?
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?
English is fifth lang. for me... It kinda hard... Sorry but thx for reply!
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
I am working on code right now
Can someone show me or tell me how to fix the counting the positive and negative?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 forgot to actually call the "identifying" function.

Lines 31 and 36 should be after line 39.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>

#include<string>

using namespace std;



int initialization (int []);
int identifying (int []);
const int 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
Last edited on
@OUIJ: line 14 should use "num", and "indentifying" should be called from main, not at the end of "initialization".

Also, all of the functions (except for main) should have a return type of void - there is no reason for them to return anything.
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.
OUIJ and L B can you explain little more about the last part. I am trying to understand how to do you it? So i can learn... But byway to both of you
why do you the s+1? Whenever the user input a value in and it will move on to the next one or next element?
and oh is there a way to combine all the positive num? and negative?
The s+1 is so the user sees 1, 2, 3, ... instead of 0, 1, 2, ...
oh ok i think I understand... The element is start at 0 but we (programmer) want people to see 1,2,3.... not 0..... right?
Correct, that is indeed the unfortunate state our society is in.
ok? LB can you please explain little bit more about part about positive and negative?
Pages: 12