c problem

This is a discussion on c problem within the C Programming forums, part of the General Programming Boards category; hey i need a c program for this problem. Given is a set of integers and then a sequence of ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Question c problem

    hey i need a c program for this problem.

    Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two distinct
    numbers from the set, which is closest to the query number.

    Input

    Input contains multiple cases.
    Each case starts with an integer n (1<n<=20), which indicates, how many numbers are in the set of integer. Next n lines
    contain n numbers. Of course there is only one number in a single line. The next line contains a positive integer m giving the
    number of queries, 0 < m < 5. The next m lines contain an integer of the query, one per line.



    Output
    Output should be organized as in the sample below. For each query output one line giving the query value and the closest
    sum in the format as in the sample. Inputs will be such that no ties will occur.

    Sample input
    5
    3
    12
    17
    33
    34
    3
    1
    51
    30




    Sample output
    Case 1:
    Closest sum to 1 is 15.
    Closest sum to 51 is 51.
    Closest sum to 30 is 29.


    thnx in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,266
    Great! What have you tried?
    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

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Question

    i have tried this

    Code:
     #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    void main()  {
      int inputs[20], numInputs;
      int queries[4], numQueries;
      int i, j, a=1, b=1;
    
      clrscr();
      printf("Enter number of inputs: ");
      scanf("%d", &numInputs);
    
      printf("\nEnter input values: ");
      for(i=0; i<numInputs; i++)
        scanf("%d", &inputs[i]);
    
      printf("\n\nEnter number of queries: ");
      scanf("%d", &numQueries);
    
      printf("\nEnter query values: ");
      for(i=0; i<numQueries; i++)
        scanf("%d", &queries[i]);
    
      for(i=0; i<numQueries; i++)
      {
        for(j=0; j<numInputs; j++)
          if (abs(queries[i] - inputs[a]) > abs(queries[i] - inputs[j]))
            a = j;
    
        for(j=0; j<numInputs; j++)
        {
          if (j == a)
            j++;
          if ((abs(queries[i] - inputs[b]) > abs(queries[i] - inputs[j])))
            b = j;
        }
    
        printf("\n\nSum of values closest to %d is %d", queries[i], inputs[a]+inputs[b]);
        a=1;
        b=1;
      }
      getch();

    but the output is little diverted from supposed output .

    instead of
    Case 1:
    Closest sum to 1 is 15.
    Closest sum to 51 is 51.
    Closest sum to 30 is 29.

    i am getting

    Case 1:
    Closest sum to 1 is 15.
    Closest sum to 51 is 67
    Closest sum to 30 is 67.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Cool

    solved..it my self....

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    That's the best teacher!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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