#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Smile Very unusual output while displaying arrays


    I have two programs ... in first program which is complete it works 100% it also has a function display.

    In second program same function display with extra parameter size does not work program freezes.(note:second program is still incomplete,but 1st i want to find out whats wrong in 2nd program)


    1st program:

    Code:
    //#include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    #define MAX 5
    void insert(int *,int pos,int num);
    void del(int *,int pos);
    void rev(int *);
    void search(int *,int num);
    void display(int *);
    
    int main()
    {
    	int arr[5],temp;
    	insert(arr,1,31);
    	insert(arr,2,43);
    	insert(arr,3,12);
    	insert(arr,4,73);
    	insert(arr,5,99);
    	printf("\n\n***Program to implement Array functions!!!***\n\n");
    	printf("\nAfter inserting \n");
    	display(arr);
    	printf("\n");
    	
    	printf("\nAfter reversing \n");
    	rev(arr);
    	display(arr);
    	printf("\n");
    
    	printf("\nAfter deleting \n");
    	del(arr,3); //0 1 2 3 4 5    so 2 is on 3rd position
    	display(arr);
    	printf("\n");
    	
    	printf("\nAfter reversing \n");
    	rev(arr);
    	display(arr);
    	printf("\n");
    	
    	getch();
    	return 0;
    }
     
     
     
    void insert(int *arr,int pos,int num)
     	{
    		int i;
    		for(i=MAX-1;i>=pos-1;i--)  //eg: shifting right         ie from 3rd to 4th .... etc ...     pos-1 becuase arrays start from 0 
    		{
    			arr[i]=arr[i-1]; //shift right till u get to pos
    			arr[i]=num;         // now you are at reqd pos
    	
    		}
    	}
    	
    void display(int *arr)
    	{
    	int i;
    	for(i=0;i<MAX;i++)
    		{
    			printf("%d\t",arr[i]);
    		}
    	}
    void rev(int *arr)
    	{
    		int i,temp;
    		for(i=MAX-1;i>=MAX/2;i--)
    			{
    				temp=arr[i];
    				arr[i]=arr[MAX-1-i]; //arr4 =arr 4-4
    				arr[MAX-1-i]=temp;
    			}
    		
    	}
    	
    void del(int *arr,int pos)
    	{
    		int i;
    		for(i=pos;i<MAX;i++)
    		{
    		arr[pos-1]=arr[pos];	// if pos is 3 so arr[2]=arr[3] shifting..
    		arr[pos-1]=0;
    		}
    	}


    Problem : in second program why i dont know display does not work....

    Code:
    //#include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    #define max1 5
    #define max2 5
    
    int* create(int);
    void sort(int *,int);
    void display(int *,int);
    int* merge(int*,int*);
    
    int *arr;
    
    int main()
    {
    int *a,*b,*c;
    printf("\nEnter elements of 1st array :\n\n");
    a=create(max1);
    
    printf("\nEnter elements of 2nd array :\n\n");
    b=create(max2);
    
    printf("\nElements of 1st array  are :\n\n");
    display(a,max1);
    
    printf("\nElements of 2nd array  are:\n\n");
    display(b,max2);
    
    getch();
    return 0;
    }
    int* create(int size)
    	{
    		int *arr,i;
    		arr=(int *)malloc(sizeof(int)*size);
    		for(i=0;i<size;i++) //u could have given max1 but if u hardcode u cannot take 2nd array size value ......
    			{
    			printf("Enter the %d element of array",i+1);
    			scanf("%d",&arr[i]);	
    			}	
    	}
    
    
    void display(int *arr,int size)
    	{
    	int i;
    	for(i=0;i<size;i++)
    		{
    			printf("%d\t",arr[i]);
    		}
    	}

    Note: I have mentioned about 1st program just to tell that previous program had same function that worked but here it isnt working . :(
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,891
    Rep Power
    481
    create doesn't return a value. Turn on your compiler warnings and pay attention to them.

    (That is, create doesn't choose the value it returns.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    Originally Posted by b49P23TIvg
    create doesn't return a value. Turn on your compiler warnings and pay attention to them.

    (That is, create doesn't choose the value it returns.)
    Can you tell me how can i fix it?..me little confused
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    thanks for finding that mistake .. it worked .. just a return statement was missing

IMN logo majestic logo threadwatch logo seochat tools logo