Hello, good day. :)

I got a problem here in displaying the items of array. In "Display Items", it should display the numbers that I inputted in the "Input Items". Is there lacking in my code?

Menu:
[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/menu.png[/IMG]

Input Items:
[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/input.png[/IMG]

Display Items:
[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/result.png[/IMG]

Here's the code:

#include<stdio.h>
#include<conio.h>

int input(void);
void display(int n);

int main()
{
 int cho, i, n;
 do{
  printf("\n\n\n [1] Input Items\n");
  printf(" [2] Display Items\n");
  printf(" [3] Exit\n\n");
  printf(" Enter your choice:");
  scanf("%i",&cho);
  i = getchar();
  if(cho==1)
  {
   n = input();
  }
  if(cho==2)
  {
   display(n);
  }
 }while(cho != 3);
  printf("\n\n\t\t\t    press enter when ready");
  i = getchar();
  ++i;
  //clrscr();
  return 0;
}

int input()
{
 int a[100],i,n;
 //clrscr();
 printf("\n\n Enter integer for total numbers to be sorted: ");
 scanf("%i",&n);
 printf("\n\n");
 for(i=0;i<n;i++)
 {
  printf(" Enter integer for no.%i : ",i+1);
  scanf("%i",&a[i]);
 }
 return n;
}

void display(int n)
{
 int a[100],i;
 //clrscr();
 printf("\n\n The elements are: ");
 for(i=0;i<=n-1;i++)
 {
  printf("%i", a[i]);
 }
}

The problem is that the array is local to each function. The array in the input() function is destroyed as soon as input() exits. And the array in the display() function contains just random values because it is not the same array as that declared in the input() function.

What you need to do is declare the array in main() then pass it as argument to input() and display(). That way the array will not get destroyed between function calls.

Hello Ancient Dragon, good day. :)

How will I pass it as argument to the input() and display () functions?

Sorry but I really novice with this. I'm still a beginner. Can you give me a little code on how to pass it?

Hope you guys help me with this.

Here is an example.

int input(int a[]); // function prototype

int main()
{
   int a[100];

   int n = input(a);
}

int input(int a[])
{

}

Edited 6 Years Ago by Ancient Dragon: n/a

Hello Ancient Dragon,

Can you explain this to me -->

int input(int a[])

What's the difference between declaring inside the

{
int a[];
}

The first one declares a parameter to the function. Actual storage for the array is declared in main().

The second one declares th array inside the function input() and the array is destroyed as soon as input() returns to whatever called it.

Hello Ancient Dragon,

Here's my code again and I can't really figure it out. Still the "display()" function won't work.

#include<stdio.h>
#include<conio.h>
int input(int a[]);
void display(int n);
int main()
{
 int a[100],cho,i,n;
 do{
  printf("\n\n\n [1] Input Items\n");
  printf(" [2] Display Items\n");
  printf(" [3] Exit\n\n");
  printf(" Enter your choice:");
  scanf("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   input(a);
  }
  if(cho==2)
  {
   display(n);
  }
 }while(cho != 3);
  printf("\n\n\t\t\t    press enter when ready");
  i = getchar();
  ++i;
  clrscr();
  return 0;
}

int input(int a[])
{
 int i,n;
 clrscr();
 printf("\n\n Enter integer for total numbers to be sorted: ");
 scanf("%d",&n);
 printf("\n\n");
 for(i=0;i<=n-1;i++)
 {
  printf(" Enter integer for no.%d : ",i+1);
  scanf("%d",&a[i]);
 }
}

void display(int n)
{
 int a[100],i;
 clrscr();
 printf("\n\n The elements are: ");
 for(i=0;i<=n-1;i++)
 {
  printf("%d",a[i]);
 }
}

You are passing only 'n' (the total number) and not the actual integer array to the arguments of the display function.
What you are displaying now is the local array in the display function that contains only garbage.

So, pass the array like you have passed in the input function and the for loop counter you can replace with :

for(i=0;i<=sizeof(a);i++)

or you can also pass the integer n along with it.

Check both the options, it will be good learning for you...

Hello kings itra,

Thanks for helping me and your post is very helpful. I'm almost there but there's a bit problem in display() function.

I inputted 4 number of arrays: 4 3 2 1

But when I try to display it in "display()" function it will show only 3 numbers: 4 3 2

Here's my code again:

#include<stdio.h>
#include<conio.h>
int input(int a[],int n);
int display(int a[],int i);
int main()
{
 int a[100],cho,i,n;
 do
 {
  printf("\n\n\n [1] Input Items\n");
  printf(" [2] Display Items\n");
  printf(" [3] Exit\n\n");
  printf(" Enter your choice:");
  scanf("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   input(a,n);
  }
  if(cho==2)
  {
   display(a,i);
  }
 }
 while(cho != 3);
 {
  printf("\n\n\t\t\t    Press Enter to Exit");
  i = getchar();
  ++i;
 }
 clrscr();
 return 0;
}

int input(int a[],int n)
{
 int i;
 clrscr();
 printf("\n\n Enter integer for total numbers to be sorted: ");
 scanf("%d",&n);
 printf("\n\n");
 for(i=0;i<=n-1;i++)
 {
  printf(" Enter integer for no.%d : ",i+1);
  scanf("%d",&a[i]);
 }
}

int display(int a[],int i)
{
 int o=a[100];
 clrscr();
 printf("\n\n The elements are: ");
 for(i=0;i<=o;i++)
 {
  printf("%3d",a[i]);
 }
}

Edited 6 Years Ago by SHENGTON: n/a

Your function display() reuses the parameter variable which is supposed to be the maximum valid index value of the array. Your function destroys that and uses it for a loop counter.

void display(int a[], int max)
{
   int i;
   for(i = 0; i < max; i++)
   {

   }
}

Edited 6 Years Ago by Ancient Dragon: n/a

Hello Ancient Dragon,

Thanks for replying Sir. So what should I do? Do I have to change or add something?

Please guide me.

This article has been dead for over six months. Start a new discussion instead.