Array question HELP!!

This is a discussion on Array question HELP!! within the C Programming forums, part of the General Programming Boards category; I need to write a program that accepts an integer n, where n is the number of elements going into ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    39

    Array question HELP!!

    I need to write a program that accepts an integer n, where n is the number of elements going into the array. Have the user input n, integers. Have a function that ouputs the current array. And have another function that bubblesorts the array. Then have the output function, output the sorted array.

    I have written this code thus far, however. When compiled and run, it will ask for n, but there will be a blank line as if asking for something else, then it will run the loop, but for the %d integers in the print line, it'll have some bogus number "2293212" and then only accept 3 inputs and then crash.

    Any help would be greatly appreciated, thanks.
    Last edited by evangela; 11-28-2009 at 11:38 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf wants pointers. So you need to pass it the address of [b]n[/n], not the value of [b]n[/n]. (The value of n is unspecified, because you didn't initialize it.
    Code:
    scanf( "%d", &n );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    caught that error before you responded, still does the exact same thing. Gives a bogus number and takes 3 inputs and then closes.
    Last edited by evangela; 11-28-2009 at 11:38 AM.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    so I fixed the bogus number problem by removing the & before the n in the print statement.

    But it still has an extra place for the user to input something after getting "n" before the loop starts and it's still always only accepting 3 inputs and then crashing.
    Last edited by evangela; 11-28-2009 at 11:38 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Quote Originally Posted by evangela
    so I fixed the bogus number problem by removing the & before the n in the print statement.
    I suggest that you compile with warnings turned on to a suitable high level (e.g., at least the -Wall option for gcc and at least the /W3 option for MSVC). This should result in a warning that reveals that you did not finish doing what quzah suggested.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Very wise words indeed. I don't know how to make it stricter because im using bloodshed DEV C++ compiler, but seeing that you said I didn't do everything quzah suggested, I added an & before the a[k];. Now it accepts the correct number of integers.

    Now theres a new problem, after it executes the first function, it pauses, and doesn't do anything. Program doesn't continue. I have added the function to the code.
    Last edited by evangela; 11-28-2009 at 11:38 AM.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    Ok, the whole program seemingly executes now, however sometimes it freezes after the first function and doesn't go anywhere, and also there is something wrong with my bubble sorting function. It does not sort every number from lowest to highest like it's supposed to. This is the function.
    Last edited by evangela; 11-28-2009 at 11:38 AM.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    39
    nevermind fixed everything myself, thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21