Thread: Arrays as function arguments :(

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Arrays as function arguments :(

    Hi All!!!

    My task is...
    "Write a function that adds the elements of two one-dimensional arrays of same size. The results should be stored in a third function. The function should take as arguments the name of the three arrays and the array dimensions.

    Write a simple program that uses the function. The two arrays should be declared at the beginning of the program (in the main function block)"

    This is my code so far...

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
    	char first[31];   
    	char second[31];   
    	char name[62];	  
    
    	
    	printf("Please enter your first name(s): ");
    	scanf("%[^\n]", first);
    	printf("Please enter you surname(s): ");
    	fflush(stdin);
    	scanf("%[^\n]", second);
    
    	
    	strcpy(name, first);
    	strcat(name, " ");
    	strcat(name, second);
    
    	
    	printf("Your full name is %s", name);
    	
    }
    I have tried for ages but I cannot arrange my code in to the specified function as the task requires.

    Can anyone please help??

    Thanks in advance

    Stuart

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) Don't double post, add to the original thread.
    2)
    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    Isn't used.
    3)
    Code:
    fflush(stdin);
    Look at the FAQ as for why this is wrong.
    4) Give the function a try and then post your function's code.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    yeah sorry! i realised i made a new thread - new to message boards

    ive been working on this and a couple of other problems all day m8 - cut me some slack. ive tried 2 get this in 2 a function that many times you wudnt believe it! i really need 2 learn and practice functions but i just dnt get it when u pass arrays - my code never seems 2 compile. u guys r my last resort.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    So show us your tries!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Code:
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
    	char first[31];   
    	char second[31];   
    
    	printf("Please enter your first name(s): ");
    	scanf("%[^\n]", first);
    	printf("Please enter you surname(s): ");
    	fflush(stdin);
    	scanf("%[^\n]", second);
    
    add(first, second);
    	
    }
    add(char first, char second)
    {
    	char name[62];	 
    
    	strcpy(name, first);
    	strcat(name, " ");
    	strcat(name, second);	
    
    	printf("Your full name is %s", name);
    }
    this try actually will compile - it does so with errors and also crashes when you run it - i dunno wats wrong

    Can anyone help please?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You're still flushing stdin...a definite no-no. Other, than that, well, I think you want add to be :

    Code:
    add(char* first, char* second)
    Lastly, prototype your functions. That should get rid of some warnings/errors.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Unhappy

    Dear Salem

    I looked at your guidance for the other source code and tried to apply the same general setting for this code but this particular piece of code crashes on me.

    Ive been trying to fix it for ages, just cant seem to get it to work. atleast, i can get it to work but not how the question asks for it.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    what do i write instead of fflush then to flush the keyboard???

    otherwise, if i leave it out - the program makes up its own random names after the first string variable has been typed in.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    But apparently, the "copy/paste" rule seems to have evaded your attention span.

    char first[31];
    char second[31];

    followed by
    add(first, second);

    followed by
    add(char first, char second) // How is this a copy/paste of the arrays you're passing???


    FYI
    void foo ( char array[SIZE] );
    void foo ( char array[ ] );
    void foo ( char *array );
    are all equivalent.
    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.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    ohhhh okay! nice 1 ppl!!
    thanks 4 help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM