Thread: Arrays

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

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Arrays


    Can someone please help me with this ?
    I'm on with a school project and just started c programming.
    Please excuse my stupid questions.

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

    #define MAX 5

    void insert(int*,int pos, int num);
    void del(int*, int pos);
    void reverse(int*);
    void display(int*);
    void search(int*, int num);

    void main()
    {
    int arr[5];

    insert(arr,1,11);
    insert(arr,2,12);
    insert(arr,3,13);
    insert(arr,4,14);
    insert(arr,5,15);

    printf("\n Elements of the array:");
    display(arr);
    }


    output
    error : 13 C:\Users\~~~\Documents\arr.cpp `main' must return `int'

    someone please help me.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    > error : 13 C:\Users\~~~\Documents\arr.cpp `main' must return `int'
    Exactly what it says

    Change
    void main

    Into
    int main

    and put
    return 0;
    at the end of the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    #include<stdio.h>
    #include<conio.h>

    #define MAX 5

    void insert(int*,int pos, int num);
    void del(int*, int pos);
    void reverse(int*);
    void display(int*);
    void search(int*, int num);

    int main()
    {
    int arr[5];

    insert(arr,1,11);
    insert(arr,2,12);
    insert(arr,3,13);
    insert(arr,4,14);
    insert(arr,5,15);

    printf("\n Elements of the array:");
    display(arr);

    del(arr,5);
    del(arr,2);
    printf("\n After deletion:");
    display(arr);

    insert(arr,2,222);
    insert(arr,5,555);
    printf("\n\nAfter insertion");

    display(arr);
    reverse(arr);
    printf("\n\nAfter reversing");
    display(arr);
    search(arr,222);
    search(arr,666);

    getch();
    return 0;
    }

    output:

    C:\Users\~~~\AppData\Local\Temp\ccIvaaaa.o(.text+0x41) In function `main':

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `display(int*)'

    [Linker error] undefined reference to `del(int*, int)'

    [Linker error] undefined reference to `del(int*, int)'

    [Linker error] undefined reference to `display(int*)'

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `insert(int*, int, int)'

    [Linker error] undefined reference to `display(int*)'

    [Linker error] undefined reference to `reverse(int*)'

    [Linker error] undefined reference to `display(int*)'

    [Linker error] undefined reference to `search(int*, int)'

    [Linker error] undefined reference to `search(int*, int)'

    C:\Users\~~~\AppData\Local\Temp\ccUDbaaa.o(.text+0x41) ld returned 1 exit status
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    Please use [code][/code] tags when posting code.

    > void insert(int*,int pos, int num);
    This is a prototype.

    This is an implementation.
    Code:
    void insert(int*,int pos, int num) {
        // do stuff with the parameters
        // preferably something compatible with the name of
        // the function.
    }
    You need both of them in a full program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    I don't understand :( :confused:
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    You're calling functions (eg, insert). Where do you actually implement those functions?

    If you have not implemented those functions, then they do not exist. You are calling imaginary functions that do not exist and the linker will complain most loudly when you try to do something that delusional. As it is complaining.

    What's not to understand?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Can you please fix my program and paste it ??
    I really need it fast as possible...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    67
    Rep Power
    0
    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;
    		}
    	}
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by shannondias
    Can you please fix my program and paste it ??
    I really need it fast as possible...
    First you need to do either one of two things:

    1. Physically come to within arm's length of us so that our fingertips can achieve physical contact with your cranium, since that is a minimal requirement to perform a mind-meld with you so that we can have some chance of reading your mind.

    or

    2. Answer our questions and provide us with the necessary information!

    Your choice, but be forewarned that we have not been maintaining our mind-meld skills.

    I repeat again!:
    You're calling functions (eg, insert). Where do you actually implement those functions?

    If you have not implemented those functions, then they do not exist. You are calling imaginary functions that do not exist and the linker will complain most loudly when you try to do something that delusional. As it is complaining.

    What's not to understand?
    So then answer the question!

    swapy just showed you how to do it in one single source file. Had you placed those function implementations in another source file? Did you ever bother to add that other source file to your project? If you are trying to do this with multiple source files, then you need to tell us what compiler you're using before we can advise you how to add that other source file to your project.

    Give us the information that we need and we will be able to help you. Refuse to give us that information and we will be unable to help you and no amount of whining on your part will change anything. We cannot read your mind!

    Are you beginning to understand now?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Thank you very much, I'm just a beginner so excuse my stupid questions.

IMN logo majestic logo threadwatch logo seochat tools logo