Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,396 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,114 people online right now. Registration is fast and FREE... Join Now!




Finding the closest sum

 
Reply to this topicStart new topic

Finding the closest sum

comC
23 Oct, 2006 - 10:23 AM
Post #1

New D.I.C Head
*

Joined: 23 Oct, 2006
Posts: 5


My Contributions
You have to find the closest sum by reading the data from file i tried but i cant get it to run
mu code is :
CODE

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define N 23
int main()
{
   fopen("data.dat","r";        
   int cas=1,n,i,j,h,m,z,a[N],l,dif,now,pre,b[N];
  scanf("%ld",&n);
while n!=0);      
   {
      printf("Case %d:\n",cas);cas++;
      for(i=0;i<n;i++) scanf("%ld",&b[i]);
      for(i=0;i<n;i++)
      {
         h=i;
         for(j=i+1;j<n;j++) if(b[j]<b[h]) h=j;
         j=b[h];b[h]=b[i];b[i]=j;
      }
      h=1;a[0]=b[0];
      for(i=1;i<n;i++) if(b[i]!=b[i-1]) a[h++]=b[i];n=h;
      scanf("%ld",&m);
      for(z=0;z<m;z++)
      {
         scanf("%ld",&l);
         dif=pre=abs(a[0]+a[1]-l),now=a[0]+a[1];
         for(i=0;i<n;i++)
         {
            for(j=i+1;j<n && abs(a[i]+a[j]-l)<=pre;j++)
pre=abs(a[i]+a[j]-l);
            if(pre<dif) dif=pre,now=a[i]+a[j-1];
         }
         printf("Closest sum to %ld is %ld.\n",l,now);
      }
   }

this is what the question asks

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.

Input is terminated by a case whose n=0. Surely, this case needs no processing.

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

3

1

2

3

3

1

2

3

3

1

2

3

3

4

5

6

0

Sample output

Case 1:

Closest sum to 1 is 15.

Closest sum to 51 is 51.

Closest sum to 30 is 29.

Case 2:

Closest sum to 1 is 3.

Closest sum to 2 is 3.

Closest sum to 3 is 3.

Case 3:

Closest sum to 4 is 4.

Closest sum to 5 is 5.

Closest sum to 6 is 5.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Finding The Closest Sum
23 Oct, 2006 - 10:31 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Can you please specify the problem you are encountering, inlcuding any error messages received, and a description of any behaviour that deviates from your expectations.
User is offlineProfile CardPM
+Quote Post

comC
RE: Finding The Closest Sum
23 Oct, 2006 - 10:36 AM
Post #3

New D.I.C Head
*

Joined: 23 Oct, 2006
Posts: 5


My Contributions
thats the thing i dont get any error
i put cc filemane.c
then
a.out
then the curser flashes and hat ever i enter i dont get anything.
User is offlineProfile CardPM
+Quote Post

comC
RE: Finding The Closest Sum
23 Oct, 2006 - 11:26 AM
Post #4

New D.I.C Head
*

Joined: 23 Oct, 2006
Posts: 5


My Contributions
CODE
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {

freopen("data.dat","r",stdin);
int n,INT_MAX, list[20], k, max, ans, cases = 1, i, j, m, ques[5];
scanf("%d",&n);

while (n!=1) {

for (i=0;i<n;i++)
scanf("%d", &list[i]);
for(i=0;i<n;i++)
scanf("%d", &m);
for (i=0;i<m;i++)
scanf("%d", &ques[i]);
printf("Case %d:\n", cases);
cases++;
for (k=0;k<m;k++) {
for (i=0,max=INT_MAX;i<n;i++)
for (j=i+1;j<n;j++)
if (abs(list[i]+list[j]-ques[k])<max&&list[i]!=list[j]) {
max = abs(list[i]+list[j]-ques[k]);
ans = list[i]+list[j];
}
printf("Closest sum to %d is %d\n", ques[k], ans);
}
}
return 0;
}




wont run
infinite cases 123545
casese 235356
etc.........
please some one help i am really stuck this due tonite

edit: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Finding The Closest Sum
23 Oct, 2006 - 12:25 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Part of the problem lies in this statement fopen("data.dat","r";.

You are missing the closing parenthesis, should be like this fopen("data.dat","r");
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Finding The Closest Sum
23 Oct, 2006 - 02:54 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
I have merged to the two topics as they are the same assignment.

Can you please provide a description of what is going wrong...a suggested format might be as follows:

1. Describe input
2. Describe output
3. Describe expected output if different than actual output.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month