C Programming Help
2 posters
Page 1 of 1
C Programming Help
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][/img]
Thanks. I very noob at this
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][/img]
Thanks. I very noob at this
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
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- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
- 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- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
haha the 2 other test cases are not released. sianz. i will try to look through ur code. thanks
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
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..
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..
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..
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- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
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][/img]
haha i cant get 123456 5 correct
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][/img]
haha i cant get 123456 5 correct
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
n>=1 && n<=6
k=digit%10^n/10^(n-1);
k=digit%10^n/10^(n-1);
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
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- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
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.
paiseh its in C++.. i onli got C++ compiler with me now. its almost the same. dun worry...
paiseh its in C++.. i onli got C++ compiler with me now. its almost the same. dun worry...
wyehuong- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
haha but i try 123456 2 i got 7
sianz
sianz
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
my 1 also got same error.. i think realli is formula got smth wrong
wyehuong- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
- 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- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
haha thanks i solved it on my own too. thanks a lot bro
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
hey er u all know what is switch?
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
- 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- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
Thanks
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
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- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
- Code:
for(i = 5; i < 10;)
{
y = ++i;
cout << "value of y is: " << y << endl;
}
------------------------------------------------------------------------------------------------
- Code:
for(i = 5; i < 10;)
{
y = i++;
cout << "value of y is: " << y << endl;
}
++i does not retain original value while i++ retains original value
wyehuong- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Re: C Programming Help
whoa thanks alot man
chak- Silent Protector
-
Number of posts : 757
Age : 36
Location : Fighting on
Registration date : 2008-06-03
Re: C Programming Help
chak, do u ask ur teachers abt the problems hav?
wyehuong- Divine
-
Number of posts : 1773
Age : 35
Location : Bukit Panjang, Singapore
Registration date : 2008-06-01
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|