Thread: C program on arrays - Explination

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    C program on arrays - Explination

    Hey,
    i have a code here that scans 10 integers and prints them without they're double.
    If i print 1234356792 my output will be 12345679
    The code below shows how to do it but i need someone to explain me a loop that's there.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int arr[10];
        int i, j, seen;
        
        printf("Enter 10 integers:\n");
        
        for (i=0; i<10 ; i++)
        {
            scanf("%d", &arr[i]);
        }
        printf("Set(array) is:");
        
        for (i=0; i<10; i++)
        {
            seen = 0;
            for (j=0; j<i; j++)           // What does that mean?
            {
                if (arr[i]==arr[j])
                {
                    seen=1;             // what the 1 means?
                    break;
                }
            }
            if(!seen)
                printf("%d", arr[i]);
        }
        printf("\n");
        return 0;
    }
    appreciate any help

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I can illustrate one problem. Scanf will read for exactly what you tell it to. So. How many integers is this?

    123456789 1 2 3 4 5 6 7 8 9

    If you said anything besides 10, you're wrong.

    Code:
    for (j=0; j<i; j++)           // What does that mean?
    Does your book have a section on for loops? You should read it.

    Code:
                    seen=1;             // what the 1 means?
    True, in this later Boolean expression:
    Code:
    if(!seen)
    See also <stdbool.h>

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    thanks for you reply.
    for the scanf it scans 10 integers so 10 is an integer also it could scan it of course

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    In older C versions there was no bool type. With a conformant compiler you can write
    Code:
    #include <stdbool.h>
    //...
    bool seen = false;
    // ...
    seen = true;
    // ...
    if (seen)
    Still, 1 and 0 are very common idioms for true and false, respectively. However, note that any other other than 0 is interpreted to mean "true"

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    108
    sick appreciate the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program using arrays
    By altrev in forum C++ Programming
    Replies: 1
    Last Post: 11-15-2009, 01:25 PM
  2. I need a decent explination.....
    By tetraflare in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 10:06 AM
  3. Need help with 'C' Program (arrays)
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 12:36 AM
  4. Explination?
    By tetraflare in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 11:39 AM
  5. Need example and explination...
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 09:38 PM