9 Replies - 448 Views - Last Post: 28 March 2011 - 11:10 AM Rate Topic: ***-- 2 Votes

#1 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Passing Array by Reference Help

Posted 28 March 2011 - 10:08 AM

Can I get a little help with the following program.
Apparently there is some problem with the passing Array by reference in my code.
There is a linking error.

Edit: Error File Attached

#include<iostream.h>
#include<conio.h>
void input_array(int (*A)[],int);
/*
void sel_sort(int[],int);
void buble_sort(int[],int);
void insertion_sort(int[],int);
*/
void show_array(int (*A)[],int);


void main()
	{
	int A[50];
	int size;
	char ch_1;
	clrscr();
	cout<<"Following choices are available:\n\
1. Input Array\n\
2. Sort Array\n\
3. Show Array\n\
4. Exit\n\
\n\
What do you want to do?(1-4)";
	cin>>ch_1;
	switch(ch_1)
		{
		case '1':
			input_array(&A,size);
			break;
		case '2':
			cout<<"Area Under Devolopment";
			break;
		case '3':
			show_array(&A,size);
			break;
		case '4':
			cout<<"Area Under Devolopment";
			break;
		}
	clrscr();
	main();
	getch();
	}


void input_array(int A[],int size)
	{
	cout<<"Enter the no. of size of the Array:\n";
	cin>>size;
	cout<<"\nEnter the elements of the array: ";
	for(int i=0;i<size;i++)
		{
		cout<<"Enter the Array element "<<i+1<<": ";
		cin>>A[i];
		}
	}


void show_array(int A[],int size)
	{
	for(int i=0;i<size;i++)
		{
		cout<<A[i]<<"\t";
		}
	}




Edit:
And heads up.
I had a problem with my Directories.(in c++)
The output and source directories were not accesible
and c++ showed a fatal error
saying program name.obj is not created.

I fixed that by changing the directories to valid location.
i hope that is not the problem....

Attached File(s)

  • Attached File  Error.bmp (126.49K)
    Number of downloads: 30

This post has been edited by sumit.xxx365: 28 March 2011 - 10:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing Array by Reference Help

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:10 AM

please post the error in full :)
if it is a linker error then it will have nothing to do with the code :)

and also please use int main instead of void main :/
Was This Post Helpful? 1
  • +
  • -

#3 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:20 AM

The error is attached.

Edit:
And another thing.
I had a problem with my Directories.(in c++)
The output and source directories were not accesible
and c++ showed a fatal error
saying program name.obj is not created.

I fixed that by changing the directories to valid location.
i hope that is not the problem....

Attached File(s)

  • Attached File  Error.bmp (126.49K)
    Number of downloads: 26

This post has been edited by sumit.xxx365: 28 March 2011 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:30 AM

There is a difference between
void foo(int *A[]);
// and
void foo(int A[]);


Was This Post Helpful? 2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:35 AM

Stop using 20-year-old compilers and their non-standard headers!

Stop using void main(), it's int main()! ALWAYS!

Don't recursively call main()! NEVER call main() from your code!
Was This Post Helpful? 1
  • +
  • -

#6 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:37 AM

View Postsk1v3r, on 28 March 2011 - 10:30 AM, said:

There is a difference between
void foo(int *A[]);
// and
void foo(int A[]);



I am not able to understand.
Can you explain in a bit more detail..
AS in what exactly is the difference.

View PostJackOfAllTrades, on 28 March 2011 - 10:35 AM, said:

Stop using 20-year-old compilers and their non-standard headers!

Stop using void main(), it's int main()! ALWAYS!

Don't recursively call main()! NEVER call main() from your code!


I cant help...
we are taught that way in school...
and while i am writing codes at school exams i have to use void.. :/
Was This Post Helpful? 0
  • +
  • -

#7 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:41 AM

void foo(int *A[]);
// and
void foo(int A[]);


The top one has an asterisk.
The one beneath it does not.
This asterisk denotes that the variable is a pointer.
In this case it says that A is an array of pointers that each point to an integer variable.

please do not downrep people that are offering good advice. He was stating the fact that your compiler is out of date, and according to many compilers and the creator of c++ himself void main is always wrong, and int main is always right
Was This Post Helpful? 1
  • +
  • -

#8 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:41 AM

View Postsk1v3r, on 28 March 2011 - 10:39 AM, said:

void foo(int *A[]);
// and
void foo(int A[]);


The top one has an asterisk.
The one beneath it does not.
This asterisk denotes that the variable is a pointer.
In this case it says that A is an array of pointers that each point to an integer variable.


but without using that i had more errors.
can u debug the program for me??

I'll understand what i was doing wrong by reading the code.


I apologise to him for doing that..
I at first didn't like the way he had pointed at the mistake....
and in a fit of anger i downreped him...
i regretted doing that later...
thanks for correcting my mistake :D

This post has been edited by sumit.xxx365: 28 March 2011 - 10:44 AM

Was This Post Helpful? -2
  • +
  • -

#9 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 10:47 AM

You do not need an array of pointers when passing an array. All you need is one pointer to point to the first element of the array, and the size of the array as a whole, and you can then use that pointer like an array, and modifying it will modify the original array
(just don't exceed the bounds)
E.G
void foo(int * p_arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << p_arr[i];
    }
}
int main()
{
    int my_array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int size = 10;
    foo(my_array, size);
    return 0;
}



If you still do not understand, or have more errors to give us, then reply back :)
but we will not just fix your code completely for you

This post has been edited by sk1v3r: 28 March 2011 - 10:48 AM

Was This Post Helpful? 1
  • +
  • -

#10 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Passing Array by Reference Help

Posted 28 March 2011 - 11:10 AM

View Postsk1v3r, on 28 March 2011 - 10:47 AM, said:

You do not need an array of pointers when passing an array. All you need is one pointer to point to the first element of the array, and the size of the array as a whole, and you can then use that pointer like an array, and modifying it will modify the original array
(just don't exceed the bounds)
E.G
void foo(int * p_arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << p_arr[i];
    }
}
int main()
{
    int my_array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int size = 10;
    foo(my_array, size);
    return 0;
}



If you still do not understand, or have more errors to give us, then reply back :)
but we will not just fix your code completely for you


I edited The code and have made a few changes.
I am able to run the input array function..
but when i run show_array func.
gibberish data is shown on my screen...

#include<iostream.h>
#include<conio.h>
#include<process.h>
void input_array(int *A,int);				/*
void sel_sort(int[],int);
void buble_sort(int[],int);
void insertion_sort(int[],int); 			*/
void show_array(int *A,int);


int main()
	{
	int A[50];
	int size;
	char ch_1;
	clrscr();
	START:
	cout<<"Following choices are available:\n\
1. Input Array\n\
2. Sort Array\n\
3. Show Array\n\
4. Exit\n\
\n\
What do you want to do?(1-4)";
	cin>>ch_1;
	switch(ch_1)
		{
		case '1':
			input_array(A,size);
			break;
		case '2':
			cout<<"Area Under Devolopment";
			break;
		case '3':
			show_array(A,size);
			break;
		case '4':
			exit(0);
			break;
		}
	getch();
	clrscr();
	goto START;
	getch();
	return 0;
	}


void input_array(int *A,int size)
	{
	cout<<"Enter the no. of size of the Array:";
	cin>>size;
	cout<<"\nEnter the elements of the array:\n";
	for(int i=0;i<size;i++)
		{
		cout<<"Enter the Array element "<<i+1<<": ";
		cin>>A[i];
		}
	}


void show_array(int *A,int size)
	{
	for(int i=0;i<size;i++)
		{
		cout<<A[i]<<"\t";
		}
	}




Edits:
Lines 4, 8, 17, 41, 43, 49, 62.

I noticed I couldnt see the gibberish data due to a getch() func not there before clrscr() on Line 41...

And also used int main :P :D

This post has been edited by sumit.xxx365: 28 March 2011 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1