-1

I'm trying to write the code for a C++ program which will get some numbers (integers) and will put them into a 100 sized array, and will begin searching for possibly given negative ones (Negative of given positive numbers) after the user had inputted the sentinel number (101). For example; when we give the integers 1, 45, 12, -32, 103, 2015 and 32 to the program, it should give us the the integer 32 (because the negative form of it is existing) and if there were no numbers with this statement, then it will prints nothing. I wrote something like below; but I don't know how to do the rest... Any help or suggestions are appreciated.

I forgot to say that I use CodeBlocks 13.12 .

#include <iostream>

using namespace std;

int number = 0, nCounter = 0, sentinel = 101, i;
int myArray[100];

int main (){

cout << "Please enter your numbers: " << endl;

while ( number != 101 ){

  cin >> number;
  myArray[0]= number;
  nCounter += 1;

}

for ( i = 0; i <= nCounter; i++ ){
  if (myArray[i] > 0) // I'm stuck at here!
}

return 0;
}

Thanks and please apologize for possible English mistakes.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Sepehr Student
  • 23
  • 1
  • 1
  • 6

6 Answers6

2

Here are some mistakes in the code :

  1. First, you are assigning all the input elements to the 0th indexed element of the array.

  2. The user can very well give 200 elements without typing 101, in that case you will overrun your array size.

A simple algorithm should be like this:

  1. Pick the ith positive element and search through out the array for its negative.

  2. Repeat 1 for every possible positive element in the array.

Here is a working example.

The input should be like this :

while ( (nCounter < 100) && (number != sentinel) ) {
    std::cin >> number;
    myArray[nCounter]= number;
    nCounter += 1;
}

And the checking condition:

for ( i = 0; i < nCounter; i++ ){
    if (myArray[i] > 0) {
        for( j = 0; j < nCounter; j++) {
            if(myArray[i] + myArray[j] == 0) // positive and negative add up to 0
                std::cout << myArray[i] << std::endl ;
        }
    }
}
a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

Here's a slight modification of your code that will get you what you need

#include <iostream>

using namespace std;

int number = 0, nCounter = 0, sentinel = 101, i, negMatch;
int myArray[100];

int main (){

cout << "Please enter your numbers: " << endl;

while ( number != 101 ){

  cin >> number;
  myArray[nCounter]= number;
  nCounter += 1;

}

cout << "Enter the number to negative match";
cin >> negMatch;

for ( i = 0; i < nCounter; i++ ){
  if ( (myArray[i] + negMatch) == 0) {
    cout << myArray[i];
    return 0;
  }
}

return 0;
}

Please note the following changes:

  1. You were inserting all the elements into the first slot, I changed it so that you enter them in the correct spot
  2. Getting the number to be matched as input (negMatch is "32" in your question)
  3. Modified the loop to check the numbers

However, this program is not ideal. Ideally, you would use something like Vectors, which can dynamically grow. Also, it might be better to have the user input the count of numbers, instead of using a sentinel number that he might want to give as input.

Porcupious
  • 31
  • 3
  • Thanks, I'm just learning the basics but yes, It's not an ideal one indeed. You miss-understood my question (or maybe I explained it in a bad manner) I meant that the program will search for any possible negative matches for all the positive numbers. – Sepehr Student Mar 07 '15 at 16:16
0

I suggest to write positive numbers in the beginning of the array and negative numbers in the end of the array.

Here is a demonstrative program

#include <iostream>

int main() 
{
    const size_t N = 100;
    const int SENTINEL = 101;
    int a[N];

    int number;
    size_t positive_end   = 0;
    size_t negative_begin = N;

    for ( size_t i = 0; i < N && std::cin >> number && number != SENTINEL; i++ )
    {
        if ( number < 0 )
        {
            a[--negative_begin] = number;
        }
        else
        {
            a[positive_end++] = number;
        }
    }

    if ( positive_end != 0 && negative_begin != N )
    {
        for ( size_t i = 0; i < positive_end; i++ )
        {
            size_t j = negative_begin;
            while ( j != N && a[i] + a[j] != 0 ) ++j;

            if ( j != N ) std::cout << a[i] << '\t' << a[j] << std::endl;
        }
    }

    return 0;
}

If for example to enter the following sequence of numbers

1 2 -3 4 -5 6 7 3 -9 9 101

then the output will be

3   -3
9   -9

Also you could sort each part of the array (the part of positive numbers and the part of negative numbers) and apply standard algorithm std::set_intersection. In this case you could exclude situations when one negative number corresponds to several positive numbers.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If I understand this correctly you want to print the negative ones but with positive sign. With this simple code you can do it!

#include <iostream>

using namespace std;

int number = 0, nCounter = 0, sentinel = 101;
int myArray[100];

int main (){

    cout << "Please enter your numbers: " << endl;

    while ( (nCounter < 100) && (number != sentinel) ) {
        std::cin >> number;
        myArray[nCounter]= number;
        nCounter += 1;
    }

    for (int i = 0; i < nCounter; i++ ){
        if (myArray[i] < 0) {
            std::cout << (myArray[i] * -1) << std::endl ;
        }
    }

    return 0;
}

A simple change that reduce the computational cost is the following: you can try to get information from the number given when you read it

#include <iostream>
#include <vector>

using namespace std;

int number = 0, sentinel = 101;

int main (){

    cout << "Please enter your numbers: " << endl;

    vector<int> array;

    while (number != sentinel) {
        std::cin >> number;
        if(number < 0)
            array.push_back(number);
    }

    for (int i = 0; i < array.size(); i++ )
        std::cout << (array[i] * -1) << std::endl ;

    return 0;
}
ABeltramo
  • 185
  • 3
  • 10
0

You did not pay enough attention to the logic of your code. I'll assume you are very new at this, but no person will want to enter 100 inputs before they see what your program does. Here is what's wrong with your code:

#include <iostream>

using namespace std;

int number = 0, nCounter = 0, sentinel = 101, i; // OK
int myArray[100]; // OK, an array with 100 elements

int main (){

cout << "Please enter your numbers: " << endl;

while ( number != 101 ){  //this is where you got it wrong
        // this should have been nCounter instead of number
        // If you are looking at 100 elements then the condition 
        // should be "nCounter != 100"

  cin >> number;
  myArray[0]= number; // this should have been "myArray [nCounter]=number;"
  nCounter += 1;

}

for ( i = 0; i <= nCounter; i++ ){ // defining i from outer scope is unnecessary
                                            // since it is only used in the for loop
  if (myArray[i] > 0) // I'm stuck at here! // Put a semicolon here
// the remainder of the code probably here 
} 

return 0;
}
NyproTheGeek
  • 53
  • 2
  • 7
0
#include<iostream>

using namespace std;


int main()
{
    //initialize size and empty array
    int size = 10, x;
    int myArray[10] = {};

    //enter integers into array
    for (int i = 0; i < size; i++)
    {
        cin >> myArray[i];
    }

    //search array for negative numbers
    for (int i = 0; i < size; i++)
    {
        if (myArray[i] < 0)
        {
            x = (myArray[i] * (-1));        //multiply by -1 to get (+)
            cout << x << ' ';
        }
    } 
    return 0;       
}
Dave
  • 1