passing arrays

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

52 Replies - 3521 Views - Last Post: 07 November 2010 - 10:31 PM Rate Topic: -----

#1 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

passing arrays

Posted 06 November 2010 - 07:03 PM

hey there guys, have a question on passing arrays. The goal of my program so far is just to generate a random 100 numbers from 0-100 and then print those numbers. From my knowledge so far I thought I was doing it right but apparently not. Anyone want to help me and point me in the right direction as to why Im getting errors?

thanks appreciate it.

# include <iostream>
using namespace std;

int fillArray(int [], int);
int printArray(int [], int);


void main()
{
	const int number=100;
	int randvalue[number];
	int i;

	fillArray(randvalue, number);
	printArray(randvalue, number);

}


void fillArray(int randvalue[], int number)
{

	srand(time(NULL));
	
	for (i=0; i<number; i++)
	{
		randvalue[] = (rand()%100)+1;
	}
}

void printArray (int randvalue[], int number)
{
	fillArray(randvalue[], number);
	
	for (i=0; i<number; i++)
	{
		randvalue = (rand()%100)+1;
		cout<<randvalue<<endl;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: passing arrays

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: passing arrays

Posted 06 November 2010 - 07:11 PM

When you say you are getting errors do you mean compile errors or run-time errors?
What are these errors?
Give us the details.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: passing arrays

Posted 06 November 2010 - 07:20 PM

Which element of your array to you expect to be assigning here?
randvalue[] = (rand()%100)+1;


Was This Post Helpful? 0
  • +
  • -

#4 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: passing arrays

Posted 06 November 2010 - 07:25 PM

compiler errors I believe. the attached is a view of what I am seeing. I understand some of them but what exactly is an overloaded function? and the redefinition error I dont really understand either.

View Postbaavgai, on 06 November 2010 - 06:20 PM, said:

Which element of your array to you expect to be assigning here?
randvalue[] = (rand()%100)+1;



I thought I could assign the 0-100 values to that so I could just cout<< randvalue
or is my logic wrong. Now that you point that out I guess I wouldnt be able to do that since randvalue is just 1 integer value and the array is a list of them, right?

guess it didnt load

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: passing arrays

Posted 06 November 2010 - 07:26 PM

Yep that sounds like compiler errors to me.
Copy them and paste them exactly as they are so we can see what the compiler is complaining about.
Was This Post Helpful? 0
  • +
  • -

#6 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: passing arrays

Posted 06 November 2010 - 07:29 PM

I attatched a screen shot I took if that works for you. I can copy and paste if you'd like as well.

This post has been edited by redrider11: 06 November 2010 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: passing arrays

Posted 06 November 2010 - 07:36 PM

In future it is far easier to deal with your errors if you copy-and-paste them.
Screen shots are a pain in the neck.

So here is you function prototype
int fillArray(int [], int);


And here is your function
void fillArray(int randvalue[], int number)


Are they the same or different?
Should they be the same or different?
Was This Post Helpful? 0
  • +
  • -

#8 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: passing arrays

Posted 06 November 2010 - 07:54 PM

View Postjanotte, on 06 November 2010 - 06:36 PM, said:

In future it is far easier to deal with your errors if you copy-and-paste them.
Screen shots are a pain in the neck.

So here is you function prototype
int fillArray(int [], int);


And here is your function
void fillArray(int randvalue[], int number)


Are they the same or different?
Should they be the same or different?


ok I will keep that in mind!
hmm they are different but shouldnt they be? I thought that when you have your function prototype it is usually just the type-name-(int, int); or whatever you are declaring. Im going to say by your context clues that my [] is wrong. so im just passing a integer value of arrays therefor I should just place int within the parentheses right?
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: passing arrays

Posted 06 November 2010 - 08:09 PM

Not even close.

You said it yourself.
The prototype shows the type of the function.
Focus on that thought.
What is different? What needs to change?
Was This Post Helpful? 0
  • +
  • -

#10 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: passing arrays

Posted 06 November 2010 - 08:14 PM

so I took into consideration what you both said and came up with this.
# include <iostream>
# include <ctime>
using namespace std;

int fillArray(int , int);
int printArray(int , int);


void main()
{
	const int number=100;
	int randvalue[number];
	int i;

	fillArray(randvalue, number);
	printArray(randvalue, number);

}


void fillArray(int randvalue[], int number)
{
	int i;
	srand(time(NULL));
	
	for (i=0; i<number; i++)
	{
		randvalue[number] = (rand()%100)+1;
	}
}

void printArray (int randvalue[], int number)
{
	int i;
	fillArray(randvalue, number);
	
	for (i=0; i<number; i++)
	{
		randvalue[number] = (rand()%100)+1;
		cout<<randvalue<<endl;
	}
}



but am still getting these compiler errors.


C:\CSC100\Array>cl arrays.cpp
Microsoft ® 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright © Microsoft Corporation. All rights reserved.

arrays.cpp
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning
C4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
arrays.cpp(15) : error C2664: 'fillArray' : cannot convert parameter 1 from 'int
[100]' to 'int'
There is no context in which this conversion is possible
arrays.cpp(16) : error C2664: 'printArray' : cannot convert parameter 1 from 'in
t [100]' to 'int'
There is no context in which this conversion is possible

C:\CSC100\Array>
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: passing arrays

Posted 06 November 2010 - 08:20 PM

Look at line 5
Look at line 21

Do they match?
No.

Do they need to match?
Yes.
Was This Post Helpful? 0
  • +
  • -

#12 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: passing arrays

Posted 06 November 2010 - 08:34 PM

hmm well I coded them so that they matched
int fillArray(int randvalue[] , int number);
int printArray(int randvalue [] , int number);



but i get overloaded functions.

C:\CSC100\Array>cl arrays.cpp
Microsoft ® 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright © Microsoft Corporation. All rights reserved.

arrays.cpp
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning
C4530: C++ exception handler used, but unwind semantics are not enabled. Specify
/EHsc
arrays.cpp(22) : error C2556: 'void fillArray(int [],int)' : overloaded function
differs only by return type from 'int fillArray(int [],int)'
arrays.cpp(5) : see declaration of 'fillArray'
arrays.cpp(22) : error C2371: 'fillArray' : redefinition; different basic types
arrays.cpp(5) : see declaration of 'fillArray'
arrays.cpp(33) : error C2556: 'void printArray(int [],int)' : overloaded functio
n differs only by return type from 'int printArray(int [],int)'
arrays.cpp(6) : see declaration of 'printArray'
arrays.cpp(33) : error C2371: 'printArray' : redefinition; different basic types

arrays.cpp(6) : see declaration of 'printArray'
arrays.cpp(35) : error C3861: 'fillArray': identifier not found

C:\CSC100\Array>

from my understanding the prototype should be type-name-(datatype parameters)
isnt that what I have?


-----edit fixed it so it compiles

This post has been edited by redrider11: 06 November 2010 - 08:43 PM

Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: passing arrays

Posted 06 November 2010 - 08:45 PM

int fillArray(int , int);
int printArray(int , int);

void fillArray(int randvalue[], int number)
{

void printArray (int randvalue[], int number)
{



The function definition and the function prototype do not agree. Notice the return types............


Note: void main() is wrong it should be int main()

Jim
Was This Post Helpful? 1
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: passing arrays

Posted 06 November 2010 - 08:45 PM

Show us your full code as it is now.
Was This Post Helpful? 0
  • +
  • -

#15 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: passing arrays

Posted 06 November 2010 - 08:48 PM

# include <iostream>
# include <ctime>
using namespace std;

void fillArray(int randvalue[] , int number);
void printArray(int randvalue [] , int number);


void main()
{
	const int number=100;
	int randvalue[number];
	int i;

	fillArray(randvalue, number);
	printArray(randvalue, number);

}


void fillArray(int randvalue[], int number)
{
	int i;
	srand(time(NULL));
	
	for (i=0; i<number; i++)
	{
		randvalue[number] = (rand()%100)+1;
	}
}

void printArray (int randvalue[], int number)
{
	int i;
	fillArray(randvalue, number);
	
	for (i=0; i<number; i++)
	{
		randvalue[number] = (rand()%100)+1;
		cout<<randvalue<<endl;
	}
}



hmm well now that I have it running it just keeps going ha. It doesnt shoot out the values I want, it gives me something weird with a numbers and letters. and does this until Im asked from VB if I want to close the program. Is this a memory location?
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »