C++ learning help pls

carlborg

Active member
Joined
Feb 22, 2013
Messages
190
Reaction score
0
hey im doing a school assignment and i would like some help please..

I basically need to write a progam that asks the user to input three non zero integers and determine and print if they could be the sides of a right angled triangle...

This is what i did

#include <stdio.h>

int main(void)
{
int Number1 = 0;
int Number2 = 0;
int hypotenuse = 0;


while (Number1 <= 0)
{
printf("Enter the first non-Zero Integer\n");
scanf_s("%d", &Number1);
}


while (Number2 <= 0)
{
printf("Enter the second non-Zero Integer\n");
scanf_s("%d", &Number2);
    }


while (hypotenuse <= Number1, Number2)
{
printf("Enter the Hypotenuse\n");
scanf_s("%d", &hypotenuse);
}

}
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
If you are trying to use the Pythagoras theorem to check then it's very simple you need to square and add the first two numbers and check if they equal the square of the hypothonous

Pesudo:
Int number1
Int number2
Int hypothonous

do
scanf number1
While number <= 0

do
scanf number2
While number2 <= 0

Do
scanf hypothonous
While hypothonous <=0

Int one = (number1^2) + (number2^2)
Int two = hypothonous^2

If(one == two) then
Print right angle triangle
Else
Print ..
 

T3KTONIT

Well-known member
Joined
Sep 2, 2013
Messages
308
Reaction score
5
Code:
#include <stdio.h>

int main(void)
{
   int Number1 = 0;
   int Number2 = 0;
   int hypotenuse = 0;
   printf("Enter the first non-Zero Integer\n");
   scanf_s("%d", &Number1);
   printf("Enter the second non-Zero Integer\n");
   scanf_s("%d", &Number2);
   printf("Enter the Hypotenuse\n");
   scanf_s("%d", &hypotenuse);
   if(hypotenuse == Number1*Number1 + Number2*Number2)
   {
       printf("Yes these measurements are of a Right Angle Triangle");
   }
   else
   {
     printf("No these measurements are not of a Right Angle Triangle");
   }
}
 

carlborg

Active member
Joined
Feb 22, 2013
Messages
190
Reaction score
0
hey that worked thanks :)

also now i have another question split in 2 things

a) write a program that requests the user to enter several integers then prints the smallest number, Assume that the first value to be read specifies the number of integers to be processed and is therefore not the one of the values to compare...
so in this you have to ask the user how many numbers does he need to enter. Then depending on the number inputted, ask the user to enter those integers. The program should then compare the numbers and output the smallest number.

the second question you should read a 5 digit integer – divide the integer into the 5 different digits. Then you should compare digit 1 with digit 5 and digit 2 with digit 4 – if equal say that the number is a palindrome.

Sample program 1:

Enter a 5-digit integer:  12341
Digit 1:                  1
Digit 2:                  2
Digit 3:                  3
Digit 4:                  4
Digit 5:                  1
This number is not a palindrome.

Sample program 2:

Enter a 5-digit integer: 12321
Digit 1:                  1
Digit 2:                  2
Digit 3:                  3
Digit 4:                  2
Digit 5:                  1
This number is a palindrome.

i know this is long but if you could help me it would be great <3
thanks
 

T3KTONIT

Well-known member
Joined
Sep 2, 2013
Messages
308
Reaction score
5
[member=312]carlborg[/member] the explanation was very detailed and nicely put!, i hope that my solutions will be clear and understandable to u too :p
PHP:
#include <stdio.h>
#include <limits.h>
int main()
{
	int input; //this variable we will use for numbers that we enter
	int numberofinputs; //this variable will be used for how many numbers are going to be entered
	int smallestnumber = INT_MAX; //this variable will have the smallest number (i've initialized it with INT_MAX cuz its the biggest)
	printf("How many numbers to compare? :"); // ask for inputting number of numbers to compare
	scanf("%d",&numberofinputs); //read the input
	printf("Start entering the numbers!\n"); //ask to start entering the list of numbers
	for(int i = 0; i < numberofinputs; i++)
	{
		scanf("%d",&input); //read the number
		if(input < smallestnumber) //test and see if it is smaller than the smallest known number
		smallestnumber = input; // if true, then store it!
	}
	printf("The smallest number is %d\n",smallestnumber); //show the smallest number!
	return 1;
}
PHP:
#include <stdio.h>
#include <stdlib.h>
int main()
{
	char input[5]; //this will be used for the 5digits number in character array form
	int number; // this is the same number but in integer form
	printf("Enter a 5 digit integer : "); // ask for input
	scanf("%5s",input); //read 5characters and store in input (read the 5digit number)
	number = atoi(input); //convert it to integer
	if(input[0] == input[4] && input[1] == input[3]) //test see if digit1 = digit5 and digit2 = digit4
	printf("The number %d is a Palindrome\n",number); //if true, say it is palindrome
	else
	printf("The number %d is not a Palindrome\n",number);//if not say it is not palindrome.
	return 1;
}
 

carlborg

Active member
Joined
Feb 22, 2013
Messages
190
Reaction score
0
T3K link said:
[member=312]carlborg[/member] the explanation was very detailed and nicely put!, i hope that my solutions will be clear and understandable to u too :p


i edited a bit the first program to this

#include <stdio.h>

int main(void)
{
int input;
int numberofinputs;
int smallestnumber = 0;


printf("Enter the total numbers you wish to enter....");
scanf_s("%d", &numberofinputs);

printf("Enter the numbers\n");
for (int i = 0; i < numberofinputs; i++)
{
scanf_s("%d",&input);
if (input < smallestnumber)
smallestnumber = input;
}

printf("The smallest number is  \n");
system("pause");
return 0:
}

is there a way to make the second programm simpler using only #include <stdio.h> and int main(void)
also we only used int , and havent used char , also the scanf is scanf_s

else thanks for helping me , really helpfull  :forever_hurra:
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
carlborg link said:
i edited a bit the first program to this

#include <stdio.h>

int main(void)
{
int input;
int numberofinputs;
int smallestnumber = 0;


printf("Enter the total numbers you wish to enter....");
scanf_s("%d", &numberofinputs);

printf("Enter the numbers\n");
for (int i = 0; i < numberofinputs; i++)
{
scanf_s("%d",&input);
if (input < smallestnumber)
smallestnumber = input;
}

printf("The smallest number is  \n");
system("pause");
return 0:
}

is there a way to make the second programm simpler using only #include <stdio.h> and int main(void)
also we only used int , and havent used char , also the scanf is scanf_s

else thanks for helping me , really helpfull  :forever_hurra:

The first program T3 wrote won't work because the program might not reach the if statement in the for loop. You need a solution that will always work with any value negative or posative.
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
Here is program one, although I know I am not supposed to write for you the program and I want you to learn it by your self but I hope you could learn from it.

Code:
#include <stdio.h>

int main()
{
	int count = 0, number = 0, smallestNumber = 0;

	printf("Please enter how many number you would like to compare: ");
	scanf_s("%d", &count);

	for(int i = 0; i < count; i++) {
		printf("Please input number %d: ", i+1);
		scanf_s("%d", &number);

		if(i == 0)
			smallestNumber = number;
		else if(number < smallestNumber)
			smallestNumber = number;
		else
			continue;
	}

	printf("The smallest number is: %d", smallestNumber);

	return 0;
}
 

aDRENOID

Active member
Joined
Jan 2, 2016
Messages
28
Reaction score
4
Code:
#include <stdio.h>
int main()
{
int input[5];
int number = 0;
int pow_ten[5];
pow_ten[0] = 1;
for(int i = 1; i< 5; i++)
{
	pow_ten[i] = pow_ten[i-1] * 10;
}

printf("Enter a 5 digit integer");	

for(int i = 0; i < 5; i++)
{	
	printf("\nNumber N.%d : ", i+1);
	scanf("%d", &input[i]);
	number += (input[i] * pow_ten[i]);
}

if(input[0] == input[4] && input[1] == input[3])
{
	printf("\nThe number %d is a Palindrome\n",number);
}
else
{
	printf("\nThe number %d is not a Palindrome\n",number);
}
return 1;
}
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
Here is the second program.

Code:
#include <stdio.h>

int main()
{
	char digits[6] = {0};

	printf("Enter a 5-digit integer: ");
	
	scanf("%s", digits);

	for(int i = 0; i < 5; i++)
		printf("Digit %d\t\t%c\n", i+1, digits[i]);

	if(digits[0] == digits[4] && digits[1] == digits[3]) {
		printf("This number is a palindrome.");
	} else {
		printf("This number is not a palindrome.");
	}

	return 0;
}
 

aDRENOID

Active member
Joined
Jan 2, 2016
Messages
28
Reaction score
4
0xf0rd link said:
Here is the second program.

Code:
#include <stdio.h>

int main()
{
	char digits[6] = {0};

	printf("Enter a 5-digit integer: ");
	
	scanf("%s", digits);

	for(int i = 0; i < 5; i++)
		printf("Digit %d\t\t%c\n", i+1, digits[i]);

	if(digits[0] == digits[4] && digits[1] == digits[3]) {
		printf("This number is a palindrome.");
	} else {
		printf("This number is not a palindrome.");
	}

	return 0;
}

He doesn't want char, my way is without char.
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
Then this one without char :)

Code:
#include <stdio.h>

int main()
{
	int number = 0;
	int digits[5] = {0};

	while(number < 10000 || number > 99999) {
		printf("Enter a 5-digit integer: ");
		scanf_s("%d", &number);
	}

	digits[0] = (number / 10000) % 10;
	digits[1] = (number / 1000) % 10;
	digits[2] = (number / 100) % 10;
	digits[3] = (number / 10) % 10;
	digits[4] = (number / 1) % 10;

	for(int i = 0; i < 5; i++) {
		printf("Digit %d:\t\t%d\n", i+1, digits[i]);
	}

	if(digits[0] == digits[4] && digits[1] == digits[3]) {
		printf("This number is a palindrome.");
	} else {
		printf("This number is not a palindrome.");
	}

	return 0;
}

First program:
Code:
#include <stdio.h>

int main()
{
 int count = 0, number = 0, smallestNumber = 0;

 printf("Please enter how many number you would like to compare: ");
 scanf_s("%d", &count);

 for(int i = 0; i < count; i++) {
  printf("Please input number %d: ", i+1);
  scanf_s("%d", &number);

  if(i == 0)
   smallestNumber = number;
  else if(number < smallestNumber)
   smallestNumber = number;
  else
   continue;
 }

 printf("The smallest number is: %d", smallestNumber);

 return 0;
}
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,125
Reaction score
151
0xf0rd link said:
The first program T3 wrote won't work because the program might not reach the if statement in the for loop. You need a solution that will always work with any value negative or posative.
why wouldn't it reach it? I'm not an expert but to my understanding if the "input" is higher than the "smallestnumber" then "smallestnumber" won't be overwritten and the loop will carry on, please correct me if I'm wrong
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
monday link said:
why wouldn't it reach it? I'm not an expert but to my understanding if the "input" is higher than the "smallestnumber" then "smallestnumber" won't be overwritten and the loop will carry on, please correct me if I'm wrong

Now that I looked at his code, he specifies INT_MAX for smallestnumber at the beginning. Yes it will continue however it's not a good practice to use INT_MAX for something like this. I think the OP's professor would agree with me on this.
 

0xf0rd

Active member
Joined
Jun 20, 2014
Messages
68
Reaction score
1
monday link said:
why is it bad practice?

What if you are learning Java or C# and I told you to convert your program from C/C++ to Java. How would you do it if you don't have INT_MAX. You can't just define it because it has a different value on different systems. Also it's not supported on older C compilers you would have to manually add the header file then include it. Limits is mostly used when you write compilers.
 

carlborg

Active member
Joined
Feb 22, 2013
Messages
190
Reaction score
0
thanks for this guys , could you comment each important line so i can have more to base on?

thank you very much :)
 
Top