EOS Forum


Join the forum, it's quick and easy

EOS Forum
EOS Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

C Programming Help

2 posters

Go down

C Programming Help Empty C Programming Help

Post by chak Fri Aug 21, 2009 3:48 am

I have done the source code for this question but there is something wrong as i only got 3/5 test cases right.

Task 1 IsSum

Given three input integers, determine if one of the number is the sum of the other two. If so, print out that number, otherwise print "No". You should terminate your final output with a new line character ("\n").
Sample Run

Sample run using interactive input (user's input shown in blue; output shown in bold purple):
3 7 5
No

Another sample run
12 7 5
12

Another sample run
7 5 12
12

Another sample run
7 5 13
No

My code
[img]C Programming Help Code10[/img]

Thanks. I very noob at this
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Fri Aug 21, 2009 7:04 am

i think u show me ur test cases and i will see whr the error is.. i look at ur sample run is ok lei
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Fri Aug 21, 2009 7:24 am

Code:
#include<stdio.h>
int main (void)
{
    // declaration (i equate them to 0 as a practice, safety reason)
    int a = b = c = 0;
    int sum1 = sum2 = sum3 = 0;

    // user input
    scanf("%i", &a);
    scanf("%i", &b);
    scanf("%i", &c);

    // computing sum
    sum1 = a+b;
    sum2 = a+c;
    sum3 = b+c;

    // if statements
    if(a != sum3) //if A is not the sum of b & c, continue testing
    {
          if(b != sum2) //if B is not the sum of a & c, continue testing
          {
              if(c != sum1) //if C is not the sum of a & b, there is nothing left to test
              {
                    printf("No.\n"); //nothing to test
              }

              else
              {
                  printf("%i", sum1); //C is the sum of the other 2 numbers a & b! print out sum1.
              }
          }

          else
          {
              printf("%i", sum2); //B is the sum of the other 2 numbers a & c! print out sum2.
          }
    }

    else
    {
          printf("%i", sum3); //A is the sum of the other 2 numbers b & c! print out sum3.
    }
   
    return 0;
}

try that.. shuld be the same as ur code.. i just rearranged it..


another method:
Code:
#include<stdio.h>
int main (void)
{
    // declaration (i equate them to 0 as a practice, safety reason)
    int a = b = c = 0;
    int sum1 = sum2 = sum3 = 0;

    // user input
    scanf("%i", &a);
    scanf("%i", &b);
    scanf("%i", &c);

    // computing sum
    sum1 = a+b;
    sum2 = a+c;
    sum3 = b+c;

    // if statements
    if(a == sum3) // is a the sum of b and c?
    {
          printf("%i", sum3); // Great! a is the sum of b and c.
    }

    else // damn it a is not the sum of b and c, move on to test the other numbers
    {
          if(b == sum2) // is b the sum of a and c?
          {
              printf("%i", sum2); // Great! b is the sum of a and c.
          }
         
          else // damn it b is not the sum of a and c, move on to test the other numbers
          {
              if(c == sum1) // is c the sum of a and b?
              {
                    printf("%i", sum1); // Great! c is the sum of a and b.
              }

              else
              {
                    printf("No.\n"); // damn it there are no 2 numbers that add up to either of the 3 numbers.
              }
          }
    }
   
    return 0; // exit

}
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Fri Aug 21, 2009 6:59 pm

haha the 2 other test cases are not released. sianz. i will try to look through ur code. thanks
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Error code

Post by chak Fri Aug 21, 2009 9:59 pm

[img]C Programming Help Ndigit11[/img]

why the error invalid lvalue in assignment come out?
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Sat Aug 22, 2009 2:27 am

sorry i cant c ur code from the screen shot i think u need to crop it esp the right side and show me the impt parts..

i tried to c some of it.. ur if statement the x part is logical but the n part is weird.. maybe i nvr c clearly but how can n be bigger or equal to 1 and be equal to 0 at the same time. 0 is smaller than 1..

hmm.. normally is i have so many parameters in my if statement.. i will try to split it into 2 if statements.. so i can spot the error easier..

Code:
if(x >= 100000 && x <= 999999) // x in the range of 100000 to 999999
{
    if(n >= 0 && n <= 1) // n in the range of 0 to 1
    {
          // code here
    }
}

and line 22.. u using modulus and square? i also rusty liao.. but then i think u shuld put some more brackets.. the compiler might hav executed the equation in a wrong sequence. modulus if i nt wrong is the remainder when u divide two integers.. correct me if im wrong

smth like..
Code:
ans = ((x%10)^n)/(10^(n-1))

ur formula.. im not sure if its supposed to work like tt.. but whenever x is divisible by 10, leaving no remainders, the ans will always be 0..


Last edited by wyehuong on Sat Aug 22, 2009 3:10 am; edited 1 time in total
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Sat Aug 22, 2009 2:53 am

Task 2 The Nth digit

Given a 6 digit number k ( 100000 ≤ k ≤ 999999), print out the the nth ( 1 ≤ n ≤ 6) digit (n is the input given by the user). If the input is not valid (e.g. k = 99999 or n = -1) then print the word invalid. The first digit is the digit on the right-most of the number.

You should terminate your final output with a new line character ("\n").
Sample Run

Sample run using interactive input (user's input shown in blue; output shown in bold purple):
375416 4
5

Another sample run:
987654 1
4

Another sample run (which is invalid because n is not between 1 and 6):
999999 -1
invalid

Another sample run (which is invalid because k is not a 6 digit number):
12345 3
invalid
Files

Your program should be coded in a single file called "NDigit.c"
Hints

* How should you get the first digit?
* You need to use a combination of two mathematical operators.

Source Code
[img]C Programming Help Ndigit12[/img]


haha i cant get 123456 5 correct
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Sat Aug 22, 2009 2:54 am

n>=1 && n<=6

k=digit%10^n/10^(n-1);
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Sat Aug 22, 2009 2:55 am

wyehuong wrote:sorry i cant c ur code from the screen shot i think u need to crop it esp the right side and show me the impt parts..

i tried to c some of it.. ur if statement the x part is logical but the n part is weird.. maybe i nvr c clearly but how can n be bigger or equal to 1 and be equal to 0 at the same time. 0 is smaller than 1..

hmm.. normally is i have so many parameters in my if statement.. i will try to split it into 2 if statements.. so i can spot the error easier..

Code:
if(x >= 100000 && x <= 999999) // x in the range of 100000 to 999999
{
    if(n >= 0 && n <= 1) // n in the range of 0 to 1
    {
          // code here
    }
}

and line 22.. u using modulus and square? i also rusty liao.. but then i think u shuld put some more brackets.. the compiler might hav executed the equation in a wrong sequence. modulus if i nt wrong is the remainder when u divide two integers.. correct me if im wrong

smth like..
Code:
ans = ((x%10)^n)/(10^(n-1))


haha dun need to say sorry. can help can le. next week got test le.
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Sat Aug 22, 2009 3:44 am

sian.. i even tried to code it on my own.. no problems.. i got the intended output of 123456 5 as 2.. i used the same formula as u.

C Programming Help 20asysm
C Programming Help Muejxz

paiseh its in C++.. i onli got C++ compiler with me now. its almost the same. dun worry...
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Sat Aug 22, 2009 6:00 pm

haha but i try 123456 2 i got 7
sianz
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Sat Aug 22, 2009 6:32 pm

my 1 also got same error.. i think realli is formula got smth wrong
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Mon Aug 24, 2009 2:38 am

haha i cant get invalid printed for 123456 7
[img]C Programming Help Ndigit13[/img]
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Mon Aug 24, 2009 3:31 am

Code:
if(x>=100000 && x<= 999999)
{
    if(n==1)
    {
          // code
    }

    else if(n==2)
    {
          // code
    }

    else if(n==3)
    {
          // code
    }

    else if(n==4)
    {
          // code
    }

    else if(n==5)
    {
          // code
    }

    else if(n==6)
    {
          // code
    }

    else // this else is for if n is equal to anything else besides 1 to 6
    {
          printf("invalid\n");
    }

}

else // this else is for x to be other than 100000 to 999999
{
    printf("invalid\n");
}

wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Mon Aug 24, 2009 6:00 am

haha thanks i solved it on my own too. thanks a lot bro
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Mon Aug 24, 2009 11:11 am

hey er u all know what is switch?
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Tue Aug 25, 2009 9:29 am

Code:
// NDigit.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <math.h>
#include <iostream>

using namespace std;

int main (void)
{
   // declaration
   int mDigit = 0;
   int n = 0;
   int mAnswer = 0;

   while(true)
   {
      cout << "Please Enter Digit: ";
      cin >> mDigit;

      cout << "Please Enter Nth Number: ";
      cin >> n;

      /*
      if((mDigit >= 100000 && mDigit <= 999999) && (n >= 1 && n <= 6))
      {
         mAnswer = mDigit % 10 ^ n / 10 ^ (n - 1);

         cout << endl << "The answer is: " << mAnswer << endl << endl;
      }

      else
      {
         cout << "Error" << endl;
      }
      */

      if(mDigit >= 100000 && mDigit <= 999999)
      {
         if(n >= 1 && n <=6)
         {
            switch(n)
            {
               case 1:
                  mAnswer = (mDigit%10)/1;
                  break;
               
               case 2:
                  mAnswer = (mDigit%100)/10;
                  break;
               
               case 3:
                  mAnswer = (mDigit%1000)/100;
                  break;

               case 4:
                  mAnswer = (mDigit%10000)/1000;
                  break;

               case 5:
                  mAnswer = (mDigit%100000)/10000;
                  break;

               case 6:
                  mAnswer = (mDigit%1000000)/100000;
                  break;
            }
            
            cout << endl << "The answer is: " << mAnswer << endl << endl;

         }

         else // if n is not within range
         {
            cout << "Invalid" << endl;
         }
      }

      else // if mDigit is not within range
      {
         cout << "Invalid" << endl;
      }
   }

   return 0;
}


this is switch
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Wed Aug 26, 2009 12:09 pm

Thanks
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Tue Sep 22, 2009 4:58 pm

hey er can tell me the diff betw prefix --i and postfix increment i++ when apply to for/ while loops? Many thanks in advance.
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Wed Sep 23, 2009 3:40 am

Code:

for(i = 5; i < 10;)
{
   y = ++i;
   cout << "value of y is: " << y << endl;
}

C Programming Help N5smyg


------------------------------------------------------------------------------------------------


Code:

for(i = 5; i < 10;)
{
   y = i++;
   cout << "value of y is: " << y << endl;
}

C Programming Help 205hxu

++i does not retain original value while i++ retains original value
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by chak Wed Sep 23, 2009 3:10 pm

whoa thanks alot man
chak
chak
Silent Protector
Silent Protector

Male
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by wyehuong Sat Oct 03, 2009 3:27 pm

chak, do u ask ur teachers abt the problems hav?
wyehuong
wyehuong
Divine
Divine

Male
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01

Back to top Go down

C Programming Help Empty Re: C Programming Help

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum