Having trouble assigning value to an array

Pages: 123
Dec 8, 2008 at 7:16am
I'm trying to assign a value to an array. To be specific I'm trying to prompt the user to put in multiple numbers, which I want to assign to their respective parts in an array.
For example:

cout<<"How many peoples age would you like to enter?";
cin >> ages
cout << "Please enter " << ages <<" ages";
Now I want that many ages to be entered into their respective places in the array.

I've tried about 10 different ways of doing it.
Usually trying to declare the array first.
etc.

If you need me to post an example of the code I will, but if you're able to explain it or it's syntax Ithink it'll be easier. I don't know why but I feel as if this isn't a really complicated question, more that i'm over looking something simple.

Thanks.
Last edited on Dec 8, 2008 at 7:17am
Dec 8, 2008 at 7:58am
Whenever the size of the array is unknown as compile time, you need to use dynamically allocated arrays -- arrays that are created in the heap, aka free store.
Here's an example:
1
2
3
//The value of n can be anything, such as user input, that is >0. It may or may
//not be known at compile time. Also, it can only be an integral value.
int *v=new int[n];
Is this your problem, or was it related to filling the array?
Dec 8, 2008 at 8:01am
actually to be completely honest, I'm having trouble with both. I didn't realize they were two questions until now.

Alright, so in your example *v is the name of the array?
And what is new? I ask because it's in blue which I assume it's significant, and i'm unaware of it.

And how would you go about filling the array?
Dec 8, 2008 at 8:18am
To be exact, arrays don't have names. Arrays are just memory set aside by you.
What does have a name, however, is the pointer pointing to one. In this case, 'v' is the name of the pointer.
You're still pretty green, so I won't confuse you with the specifics of pointer syntax. Just know the syntax I gave you to create a dynamic array.
You will access this array the same way you would access a regular (static) array: with the offset operator ([]).

new is a C++ keyword. It is an operator for sole purpose of dynamic memory allocation. The syntax above allocates space for n elements of type int and returns a pointer to the start of the array.

I would like to see what you've tried so war to fill the array.
Dec 8, 2008 at 8:21am
Alrite I'll give it a shot in my program and post back later or tomorrow.
Thanks so far.
Dec 8, 2008 at 10:01am
I'll start off with the particular function I'm working on, since I really don't have much else besides this completed :/ .
int scores()
{
int Array[25];


cout << "Enter Numbers\n" << endl;
cin >> Array[25];


return 0;

}

That's my latest attempt at trying different things. As you can see it's pretty barebones since I'm just trying to get the array to work correctly. 25 is an arbitrary number I just inserted to get the creative process going.

I've also tried establishing the Array at zero, and another variable at zero and tried to get them to work together.

For example I tried one like:

void Array (int x[]);
void printArray(const int x[]);


cout << "Enter all numbers.\n"<< endl;
Array(scores);

printArray(scores);
cout << endl;

Also I tried
setting Array and scores to 0.
So I've tried a few different things.
Please excuse my "greeness" all a learning process.

Last edited on Dec 8, 2008 at 10:03am
Dec 8, 2008 at 10:25am
Your implementation of scores is wrong because you are trying to set a value at the 26th value of your array when it has only 25 elements.
If you want to use dinamic allocation as helios suggested, read this tutorial:
http://www.cplusplus.com/doc/tutorial/dynamic.html
Last edited on Dec 8, 2008 at 10:26am
Dec 8, 2008 at 10:25am
Basic example see how this helps you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	int ages;
	
	std::cout << "Enter how big you want the array of ages to be: ";
	std::cin >> ages;

	int *myArray = new int[ages];

	for(int i = 0; i < ages; i++)
	{
		std::cout << "Enter age for element " << i << ": ";
		std::cin >> myArray[i];
	}

	for(int i = 0; i < ages; i++)
	{
		std::cout << "Age at element " << i << " in the array: " << myArray[i] << std::endl;
	}

	return 0;
}
Dec 8, 2008 at 10:27am
Oh just before the
return 0;
You could add
 
delete[] myArray;

To delete the dynamic array
Dec 8, 2008 at 10:10pm
Alright I tried using your code mythios to see if I could get it to work and then tweak it to fit into my program exactly, but right off the bat I'm already getting numerous errors that don't make any sense to me at all.
code:

int main ()
{ //Open main


int ages;

cout << "Enter how big you want the array of ages to be: ";
cin >> ages;

int *myArray = new int[ages];

for(int i = 0; i < ages; i++)
{
cout << "Enter age for element " << i << ": ";
cin >> myArray[i];
}

for(int i = 0; i < ages; i++)
{
cout << "Age at element " << i << " in the array: " << myArray[i] << endl;
}
return 0;
}

Errors:

1>c:\program files\microsoft visual studio 8\vc\include\wchar.h(257) : fatal error C1003: error count exceeds 100; stopping compilation

I got to many errors to post here and some of them I don't even understand:
1>c:\program files\microsoft visual studio 8\vc\include\new(49) : error C3646: '_THROW0' : unknown override specifier ?


what I'm going to try and do now is take your example and read that tutorial and see what I come up with.
Last edited on Dec 8, 2008 at 10:12pm
Dec 8, 2008 at 10:36pm
I think there's something wrong with my compiler.
I took a look at that tutorial and built another array and whatnot, and still got over 100 errors, all seeming to be the same as the aforementioned ones. Then for kicks I ran the example program in the tutorial and I once again got over 100 errors.

so can any one verify that the program in the tutorial is running correctly?
#include <iostream>
#include <new>
using namespace std;

int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new (nothrow) int[i];
if (p == 0)
cout << "Error: memory could not be allocated";
else
{
for (n=0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n<i; n++)
cout << p[n] << ", ";
delete[] p;
}
return 0;
}


EDIT=
Yeah there's something up with my compiler it's not even running programs that I know are correct. Any body know why I'd be getting this?
Last edited on Dec 9, 2008 at 12:28am
Dec 8, 2008 at 11:48pm
Fixed the compiler and figured out my problem...

Thanks for all the help thus far, will post back if any other questions.
Last edited on Dec 8, 2008 at 11:52pm
Dec 9, 2008 at 12:27am
Alright another problem I can't figure out:
How do you go about adding 1d arrays together.

Like you have two 1d arrays
array1 and array2

and I want to add array1[0] with array2[0] together and so on. Basically combine the two arrays into 1 array?

I need to call the two other arrays from other functions, which is another thing i'm currently looking for a solution too aswell.
Last edited on Dec 9, 2008 at 12:32am
Dec 9, 2008 at 12:43am
What do you mean "adding two arrays together"?
You mean like v3[i]=v1[i]+v2[i]?
Dec 9, 2008 at 12:59am
Yeah exactly... i think.

Like if v1[0] was say 2
and
v2[0] was say 3
then they would add together to make
v3[0] = 5

I'm trying to figure out how to get the entire array to do that.

However the both arrays are located in different functions.
So I tried:

int clapoint(Lscores, Xscores)
{
int * Point;
Point= new int [no];
int sum = 0;

for (int i=0; i<100; i++)
{
sumofscores (Lscores, Xscores)<< endl;
cout << sumofscores << endl;
}
return 0;
}
I tried calling the functions but I don't believe I'm doing it correctly.
Once again forgive my "greeness" it's all still relatively new to me.
Last edited on Dec 9, 2008 at 1:00am
Dec 9, 2008 at 2:26am
I'm trying to call it like this aswell but still no luck

int calPoint(int * Lscroes[num], int * Xscores[num])

Keeps saying:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
Dec 9, 2008 at 5:05am
Alright I got it called but I'm still having trouble getting it to add...

int calPoint(int * Lscores[], int * Xscores[])
{

int * Pscores;
Pscores = new int [no];


for (int i=0; i<no; i++)
{
* Lscores[i]+ * Xscores[i]= Pscores[i];
}
return 0;
}

having trouble with two errors, the first was the one that said you can't add two pointers together, so I put the * up and now I get -

error C2106: '=' : left operand must be l-value
Last edited on Dec 9, 2008 at 5:06am
Dec 9, 2008 at 5:14am
Uh... That doesn't seem right.
First of all, why are you passing pointers to pointers to arrays?
Second, unless you want to overwrite one of the arrays, you need to create a third one, which you have done. I was about to say something else because I misread your code, but now that I read it right, I see something even worse.
Never, ever, EVER, do this:
a+b=c
The assignment operator doesn't work like a the equality operator, or like the equals in algebra. The result of the right operand is always assign to the left operand. NEVER viceversa.
Dec 9, 2008 at 5:21am
Ah, sorry it's late
I know the difference between = and == but I overlooked it. and that it shouldn't be a+b=c but the c = b + a, I overlooked it.

Alright but how would I go about combining the two arrays into a third array?
I understand the concept of using a for loop to add each one individually but going about it isn't exactly working the way I'd want.
and am I calling the arrays correctly because I can't get the name of the function to work in main() which leads me to believe that there's something wrong with the way I'm calling them.

Edit, actually it appears that once reversed to "c=a+b" the code doesn't spit out errors there. Now the problem lies in trying to get the function into main to execute.
Seems simple enough, and I feel like I'm overlooking something simple again.
Last edited on Dec 9, 2008 at 5:31am
Dec 9, 2008 at 5:35am
You're on the right track.
If main() can't find the function, it may be because you're defining it after main() without declaring it before main().
Example:
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
//This will not work:
int main(){
    f();
}

void f(){
    something;
}
//------------------------------------------------------------------------------
//This will work:
void f(){
    something;
}

int main(){
    f();
}
//------------------------------------------------------------------------------
//This will work:
void f();

int main(){
    f();
}

void f(){
    something;
}

Your return type is wrong. You need to return a pointer to the array: 'int *'.

You haven't answered my question of why you're passing pointers to pointers:
int calPoint(int * Lscores[], int * Xscores[])
Last edited on Dec 9, 2008 at 5:36am
Pages: 123