Arrays and Ranges

Nov 21, 2008 at 4:56pm
Hello,

I looked in the contents here but can't find any thing on this particular problem...killingthemonkey's post is close but not quite!!

I want to create a program that asks the user to enter a pre-determined amount of numbers, then they actually enter the numbers. My output should display tbout 3 or four ranges that contain their number and a caveat that identifies any numbers entered outside this range. My attempts so far have been pitiful. . .

#include<iostream>

using namespace std;

int main ()
{
int numbers[100];
int a,b,c;
b=0;


int range[4]; //allowing for 4 ranges within the array
range[0]=0; //initialising the ranges to 0
range[1]=0;
range[2]=0;
range[3]=0;

cout<<"How many numbers will be entered? ";
cin>>numbers[a];
cout<<"Enter the numbers: ";
cin>>range[b];

for (b=0;b<25;b++) // to display numbers within the first range
{


cout<<"The numbers are: "<<range[3];
}


return 0;

}


I've only shown the first range code because I generally try to do the first then duplicate for the rest with suitable changed variables of course.

I am a beginner and yes this is part of a project I have to do but our teacher is one of the superior beings that doesn't understand that beginners really have to work their heads around this material. Also the course that I am doing seems severely condensed and seems to skip some basic stuff. . . .I am reading everything I can find to teach myself but so far no luck. . .hope you guys can help me. ..

Thanks
Nov 21, 2008 at 5:19pm
Let's start with how cin works. Here's a small example:

1
2
3
4
// declare a variable to hold one int
int number;
// accept a number followed by a newline from the user and put it in number
cin >> number;


Note that if the user would type "45 51 38" and then enter, cin would only grab the first consecutive numeric digits storing only 45 in number.

If you want to accept 10 numbers, you could use cin 10 times. If you want to accept a line of space-delimited numbers all at once, you would have to read a string and then parse out the numbers explicitly afterward (a little more work).

Next, let's look at a simple array:

1
2
3
4
5
6
7
8
9
// declare an array of 5 ints (indices 0-4)
int numbers[5];
// assign values to the individually indexed ints
numbers[0] = 10;
numbers[1] = 11;
// ...snip...
numbers[4] = 14;
// accept a single number from the user and store it in numbers[0]
cin >> numbers[0];


The array of 5 integers, numbers, only holds a single int per index. I'm not sure if this is repetative for you, or not, but I hope something here helps out.
Nov 21, 2008 at 5:39pm
Thanks for the advice. . . but a little clarification request

First accepting space delimited numbers using cin. . .and leaving them on the screen so the user can see what he/she has input. . . yet being able to access the input numbers for the program to use later, how do you do that?

Secondly. . .can index(element) in an array can only fit one int . . .that is
number[0] holds only one int? is there not a way to allow it to hold well any amount ie a range? Perhaps to grab all numbers between 1 and 10 previously input by the user and ignore all others then display it?


But thanks so far. . .

Nov 21, 2008 at 5:57pm
I didn't try it, but I think this will work:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <sstream>

string input;
cin >> input;
ostringstream parser( input );
int number[2];
parser >> number[0];  // first number
parser >> number[1];  // second number 


In this case number is a two element array which holds 2 ints: the first number (referenced by number[0]) and the second number (referenced by number[1]). Arrays can hold multiple values, but each element (using the [index]) only refers to one.
Nov 21, 2008 at 7:33pm
OK. . .

Now if i wanted to make my cin>>numbers to be the qantity of elements in the array, how would I pass this. . . .

cout<<"How many numbers will be entered?";
cin>>numbers;
cout<<"Enter the numbers: ";
cin>>range;



I want range to end up as an array but I also want the input by the user for numbers to be the elements for range. . .how could that be done?

Thanks
Nov 21, 2008 at 8:33pm
1
2
3
4
5
6
7
8
9
10
11
    int numbers;
    cout<<"How many numbers will be entered? ";
    cin>>numbers;

    int range[numbers];

    for( int i = 0; i < numbers; ++i )
    {
        cout<<"Enter number "<<i+1<<": ";
        cin>>range[i];
    }
Nov 21, 2008 at 9:11pm
No. numbers is not a constant expression.

You can use a pointer and new, maybe.
Nov 21, 2008 at 9:15pm
Some compilers such as GCC will allow a variable-sized array. Not sure what compiler the poster is using. (Guessing not msoft which doesn't allow it since not including stdafx.h).
Nov 21, 2008 at 9:23pm
Using Codeblocks. . .any help or expansion on seymores help would be fantastic. . .

Thanks guys
Nov 22, 2008 at 12:20am
I use VC++ without stdafx.h all the time...anyway, I would do a search for the "new" operator and arrays.
Nov 22, 2008 at 5:47am
Ah, I thought msoft compilers keyed on stdafx.h and ignored everything until it saw that (but last msoft compiler I used was VS 6).
Topic archived. No new replies allowed.