Thread: arrays problem

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    12

    Question arrays problem

    looking at the program following,i don't know how the line7 is working(what's the j variable advantages?!)!!! i'm trying on arrays and know the concepts but i don't know exactly how to use them(of course in a more complex programs in which i should use more than one array and loops togather!!

    Code:
    main()
    {
        int i,a,j,num[10];
        for(i = 0;i < 10;i++)
            scanf("%d",&num[i]);
    
       for(j = 0;j < 9;j++)
        for(i = 0;i < 10-j;i++)
            if(num[i] > num[i+1]){
                a = num[i];
                num[i] = num[i+1];
                num[i+1] = a;
            }
            printf("\n\n");
                for(i = 0;i < 10;i++)
                    printf("%d\n",num[i]);
    
    }
    tnx.

  2. #2
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    The purpose of the 'for' loop at line 7 is to execute statements 8-13 multiple times(in this case 9 times). This is done so that we end up with a sorted array.

    If you have a hard time trying to understand the algorithm, I suggest you to take a piece of paper, write down 10 numbers and see what happens to the arrangement of the numbers after each iteration.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hamidhqs
    i don't know how the line7 is working(what's the j variable advantages?!)
    Since j is not modified in the loop body, it is easy to see that this means that you have a loop that loops 9 times. As for why... trace what happens, perhaps by stepping through with a debugger.

    Quote Originally Posted by hamidhqs
    i'm trying on arrays and know the concepts but i don't know exactly how to use them(of course in a more complex programs in which i should use more than one array and loops togather!!
    I don't think there's a cure for this other than more practice and exposure to algorithms that involve arrays.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    i know that the first loop loops 9 times, but the problem is here that i think these two loops loop too much(the first loops 9 time and within it the second one loops and these are multipling to each other). i think it was enough if we had just the second one - ofcourse with "for(i=0;i<10;i++)"- that loops the same as array size!!!(unfortunatly if it would be as this,it doesn't work) the program order the numbers..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That just means that you are mistaken when you "think it was enough if we had just the second one"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2012
    Location
    Bengaluru
    Posts
    1
    If you have only second loop, then during the completion of that loop, only first largest element will be found out and placed in the right position.
    For sorting other elements we are using first for loop.
    Note the second for loop. here the second loop iterates till "10-j". So when j=1, first largest element will be found out
    and when j=2 second largest element will be found out and so on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with arrays
    By implor in forum C Programming
    Replies: 9
    Last Post: 06-01-2009, 06:12 AM
  2. Problem with arrays
    By bgavran3 in forum C++ Programming
    Replies: 12
    Last Post: 07-25-2008, 05:26 PM
  3. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 03:06 AM
  4. arrays problem
    By swapnil_sandy in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 04:14 PM