#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Question on c programming


    i was doing this programming and i am kinda stuck. Also i have no idea where did i made a mistake. Can somebody help me out with this code?

    #include<stdio.h>
    #define arrsize 5

    int main(void)
    {
    int i,r,l,list[5],sum,targetnumber;
    for (i=0; i<5; i++)
    {
    printf("Please enter a number:");
    scanf("%d", &list[i]);
    }
    printf("Please enter a target number:");
    scanf("%d",&targetnumber);

    l = 0;
    r = arrsize-1;
    while(l < r)
    {
    if (list[l] + list[r] == targetnumber)
    {

    {
    printf("Pair %d and %d.\n", list[l],list[r]);
    }
    }
    else if (list[l] + list[r] < targetnumber)
    {
    l++;
    }
    else (list[l] + list[r] > targetnumber);
    {
    r--;
    }
    }
    if (list[l]+list[r]!=targetnumber)
    {
    printf("No pair sum equal to the target number");
    }
    return 0;
    }

    & i am supposed to print the pair of numbers that sums equal to the target number.
    i would really appreciate if somebody can help.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by nirvana_1
    & i am supposed to print the pair of numbers that sums equal to the target number.
    i would really appreciate if somebody can help.
    That makes no sense, especially when your code appears to be requesting 5 numbers not "a pair".

    Explain (perhaps using the same words given in the actual assignment) exactly what requirements of the program are, and then explain what your code does or does not do in that respect.

    Finally post the code in CODE TAGS to preserve formatting.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Smile


    Code:
    #include<stdio.h>
    #define arrsize 5
    
    int main(void)
    {
    int i,r,l,list[5],sum,targetnumber,count=0;
    for (i=0; i<5; i++)
    {
    printf("Please enter a number:\n");
    scanf("%d", &list[i]);	
    }
    printf("Please enter a target number:\n");
    scanf("%d",&targetnumber);
    
    l = 0;
    r = 1; //5-1=4
    while(l < r && r<5) //0<4
    {
    if (list[l] + list[r] == targetnumber)  //only this condition is sufficient for your program..
    {
    printf("Pair %d and %d.\n", list[l],list[r]);
    count++;
    }
    l++;
    r++; 
    
    }	
    printf("found %d pairs \n",count);
    if (count==0) //here while exits and increments l  and r to next location so we do l-1 and r-1
    {	
    printf("No pair sum equal to the target number");
    }
    return 0;
    }
    here i started l from 0 and r from 1 but always kept l<r and r<5

    i used count variable to count instances... if 0 then no pair found
    thats it and it worked
    Last edited by swapy; February 4th, 2013 at 01:29 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0
    finally its working :)
    Last edited by swapy; February 4th, 2013 at 01:30 AM.

IMN logo majestic logo threadwatch logo seochat tools logo