Thread: What is wrong on last line of this code???

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    New Mexico
    Posts
    10

    Question What is wrong on last line of this code???

    I am in the process of writing a program that contains sets A and B and a universal set U. The code must first check that A and B are part of the universal set. Then I must find the Union, Intersection, difference, complement (of only A), symmetric difference, and the Cartesian product of sets A and B.Below is my code that I have done so far. I am getting an error that says "expected declaration or statement at end of input" on my last line. Can you tell me what is wrong? My code for the difference is also not working or printing.

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int k,s,n, i=0,j,size=20;
    int U[20], A[20],B[20], C[20];
    
    
    printf ("Enter the elements (Integer Between -100 and 100) for the universal set :");
    for (i=0;i<20;i++)
    
    {
        scanf ("%d",&U[i]);
    }
    
    printf ("Enter the elements (Integer Between -100 and 100) for set A :");
    for (i=0;i<20;i++)
    
    {
        scanf ("%d",&A[i]);
    }
    
    printf ("Enter the elements (Integer Between -100 and 100) for the set B :");
    for (i=0;i<20;i++)
    
    {
        scanf ("%d",&B[i]);
    }
    
    printf ("Elements of the universal set you entered:\n");
    for (i=0;i<20;i++)
    {
        printf("\t%d",U[i]);
    }
    printf ("\n");
    printf ("Elements of set A you entered:\n");
    for (i=0;i<20;i++)
    {
        printf("\t%d",A[i]);
    }
    printf ("\n");
    printf ("Elements of set B you entered:\n");
    for (i=0;i<20;i++)
    {
        printf("\t%d",B[i]);
    }
    printf ("\n");
    
    printf("\nThe intersection of the two arrays are: ");
    
    
    
    
    
    
    printf ("\n The Difference of A and B are: ");
    
    for (i = 0; i < n; ++i)
    {
        C[i] = A[i] & ~B[i];
    }
    printf ("\t%d",C[i]);
    
    
    
    
    printf("\n\n\nThe union of the two arrays are: ");
    while (i<size)
    {
    k=0;
    for (j=0; j<size; j++)
    {
    if (A[i] == B[j])
    {
    printf("\t%d", A[i]);
    i++;k++;
    }
    }
    if(k==0)
    i++;
    
    }
    
    i=0;
    while (i<size)
     {
    k=0;
    for (j=0; j<size; j++)
      {
    if (B[i] == A[j])
      {
    i++;k++;
     }
    }
    if(k==0)
      {
        printf("\t%d",B[i]);
        i++;
      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What is wrong is your chaotic indentation has led you to miss a closing brace.
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
      int k, s, n, i = 0, j, size = 20;
      int U[20], A[20], B[20], C[20];
    
      printf("Enter the elements (Integer Between -100 and 100) for the universal set :");
      for (i = 0; i < 20; i++)
      {
        scanf("%d", &U[i]);
      }
    
      printf("Enter the elements (Integer Between -100 and 100) for set A :");
      for (i = 0; i < 20; i++)
      {
        scanf("%d", &A[i]);
      }
    
      printf("Enter the elements (Integer Between -100 and 100) for the set B :");
      for (i = 0; i < 20; i++)
      {
        scanf("%d", &B[i]);
      }
    
      printf("Elements of the universal set you entered:\n");
      for (i = 0; i < 20; i++) {
        printf("\t%d", U[i]);
      }
      printf("\n");
      printf("Elements of set A you entered:\n");
      for (i = 0; i < 20; i++) {
        printf("\t%d", A[i]);
      }
      printf("\n");
      printf("Elements of set B you entered:\n");
      for (i = 0; i < 20; i++) {
        printf("\t%d", B[i]);
      }
      printf("\n");
    
      printf("\nThe intersection of the two arrays are: ");
      printf("\n The Difference of A and B are: ");
    
      for (i = 0; i < n; ++i) {
        C[i] = A[i] & ~B[i];
      }
      printf("\t%d", C[i]);
    
      printf("\n\n\nThe union of the two arrays are: ");
      while (i < size) {
        k = 0;
        for (j = 0; j < size; j++) {
          if (A[i] == B[j]) {
            printf("\t%d", A[i]);
            i++;
            k++;
          }
        }
        if (k == 0)
          i++;
      }
    
      i = 0;
      while (i < size) {
        k = 0;
        for (j = 0; j < size; j++) {
          if (B[i] == A[j]) {
            i++;
            k++;
          }
        }
        if (k == 0) {
          printf("\t%d", B[i]);
          i++;
        }
    } //!! ONE HERE
    Then there is the whole "void main" and "conio.h" thing, which strongly suggests you're using obsolete compilers / books / college tutors.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    New Mexico
    Posts
    10
    Still gives me the same error "expected declaration or statement at end of input", but the code still runs so I don't think its a big deal. How come my intersection code doesn't work and my difference portion of the code doesn't even print (printf is not working) on when my code is run?

    Thanks for your help!!!

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Quote Originally Posted by jacob_76505
    Still gives me the same error "expected declaration or statement at end of input", but the code still runs so I don't think its a big deal.
    Your code doesn't compile - what makes you think it can be executed? You're just running whatever you had from the last time you were able to compile.

    Also:
    Quote Originally Posted by Salem
    Code:
        ...
        ...
        if (k == 0) {
          printf("\t%d", B[i]);
          i++;
        }
      } //!! ONE HERE
    } //<--and another
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with this line?
    By Farnaz in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2011, 07:11 AM
  2. Comment each line and convert this C++ OOP code into C++ code.
    By Shayaan_Mustafa in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2011, 01:23 PM
  3. Error in printf line,,,what's wrong?
    By mft_ika in forum C Programming
    Replies: 9
    Last Post: 03-19-2010, 08:46 PM
  4. What's wrong in this line of code?
    By marCplusplus in forum C++ Programming
    Replies: 12
    Last Post: 12-12-2001, 09:16 AM
  5. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM