# Exercises 1 - 6 Principal & practice using C++

• (2 Pages)
• 1
• 2

## 22 Replies - 1689 Views - Last Post: 25 March 2019 - 08:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=415295&amp;s=72744b9e650c432e3285cbdaa0d4af47&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 albert003

Reputation: 26
• Posts: 677
• Joined: 15-December 14

# Exercises 1 - 6 Principal & practice using C++

Posted 12 March 2019 - 02:48 PM

I was asked to show my work for the exercises from the C++ book I am using.

On the first exercise, I wasn't sure what the author meant by...
If you haven't done so already, do the Try this exercises from this chapter. I did all of the examples he showed during this chapter so I don't know if he means that or what.

```//2
//converts miles to kilometres
double results(double m);
int main()
{
double miles;
std::cout << "Convert Miles to Kilometres"
<<"\nEnter the speed in miles:\n" <<std::endl;
std::cin >> miles;
miles = results(miles);
}
double results(double m)
{
double km = m * 1.609;
std::cout << "That speed is " << km << " kilometres." << std::endl;

return km;
}
//3
int main()
{
//illegal
int double = 0;
float age 1;
std::string 365_days;
double 1name;
//legal
std::string name1;
int number;
double customer_change;
}
//4
/////int
int sizecompare(int val1,int val2);
int adding(int val1, int val2);
int multiply(int val1,int val2);
int ratios(int val1, int val2);
int main()
{
int val1;
int val2;
int total;
std::cout << "Enter two numbers" <<std::endl;
std::cin >> val1 >> val2;
total = sizecompare(val1,val2);
std::cout << total << " is larger" << std::endl;
std::cout << "The sum of the two numbers is " << total << std::endl;
total = multiply(val1,val2);
std::cout << "Multiplying the two numbers ia: " << total << std::endl;
total = ratios(val1,val2);
//cout << "The ratio of " << val1 << " and " << val2 << " is 1:" << ratio << '\n';
std::cout << "The ratios of " << val1 << " and " << val2 << std::endl;
std::cout << "is " << total << std::endl;
}
int ratios(int val1,int val2)
{
int ans;
if(val1 < val2)
return ans = val1/val2;
else if(val1 > val2)
return ans = val2/val1;
}
int multiply(int val1, int val2)
{
return val1 * val2;
}
int adding(int val1, int val2)
{
return val1 + val2;
}
int sizecompare(int val1,int val2)
{
if(val1 < val2)
return val2;
else if(val1 > val2)
return val1;

}

//5
//float
float sizecompare(float val1,float val2);
float adding(float val1, float val2);
float multiply(float val1,float val2);
float ratios(float val1, float val2);
int main()
{
float val1;
float val2;
float total;
std::cout << "Enter two numbers" <<std::endl;
std::cin >> val1 >> val2;
total = sizecompare(val1,val2);
std::cout << total << " is larger" << std::endl;
std::cout << "The sum of the two numbers is " << total << std::endl;
total = multiply(val1,val2);
std::cout << "Multiplying the two numbers ia: " << total << std::endl;
total = ratios(val1,val2);
//cout << "The ratio of " << val1 << " and " << val2 << " is 1:" << ratio << '\n';
std::cout << "The ratios of " << val1 << " and " << val2 << std::endl;
std::cout << "is " << total << std::endl;
}
float sizecompare(float val1,float val2)
{
//https://stackoverflow.com/questions/38333489/c-calculating-the-ratio-of-2-numbers
if(val1 < val2)
return val2;
else if(val1 > val2)
return val1;
}
float multiply(float val1,float val2)
{
return val1 * val2;
}
float adding(float val1, float val2)
{
return val1 + val2;
}
float ratios(float val1,float val2)
{
//https://stackoverflow.com/questions/38333489/c-calculating-the-ratio-of-2-numbers
float ans;
if(val1 < val2)
return ans = val1/val2;
else if(val1 > val2)
return ans = val2/val1;
}
//Difference is that I can use decimal numbers and its more accurate than integers

//6
void Ascending(int n1,int n2,int n3);
int main()
{
int n1,n2,n3;
srand(time(NULL));
while(true)
{
std::cout << "Enter 3 numbers and the program" <<std::endl;
std::cout << "will organise them in descending order.\n" <<std::endl;
//std::cin >>n1 >>n2  >>n3;
n1 = 1+(rand()%3);
n2 = 1+(rand()%3);
n3 = 1+(rand()%3);
std::cout <<"The computer picks: "<<n1<<n2<<n3<<std::endl;
Ascending(n1,n2,n3);
}
}
void Ascending(int n1,int n2,int n3)
{
int temp;
if(n1 > n2)
{
temp = n1;
n1 = n2;
n2 = temp;
}
if(n2 > n3)
{
temp = n2;
n2 = n3;
n3 = temp;
}
if(n1 > n2)
{
temp = n1;
n1 = n2;
n2 = temp;
}
std::cout <<"\nSorted numbers are:"<< n1 << ","<<n2 <<","<<n3<<"\n"<<std::endl;
Sleep(1000);
}

```

This post has been edited by albert003: 12 March 2019 - 02:49 PM

Is This A Good Question/Topic? 0

## Replies To: Exercises 1 - 6 Principal & practice using C++

### #2 modi123_1

• Suitor #2

Reputation: 14925
• Posts: 59,590
• Joined: 12-June 08

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 12 March 2019 - 02:58 PM

Quote

from the C++ book I am using.

Which book?

Quote

On the first exercise, I wasn't sure what the author meant by...
If you haven't done so already, do the Try this exercises from this chapter. I did all of the examples he showed during this chapter so I don't know if he means that or what.

What part of that did the author write?

### #3 albert003

Reputation: 26
• Posts: 677
• Joined: 15-December 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 12 March 2019 - 03:51 PM

Programming Principals and Practice using C++ by Bjarne Stroutup.

### #4 jimblumberg

Reputation: 5702
• Posts: 17,481
• Joined: 25-December 09

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 12 March 2019 - 08:50 PM

Quote

On the first exercise, I wasn't sure what the author meant by...
If you haven't done so already, do the Try this exercises from this chapter. I did all of the examples he showed during this chapter so I don't know if he means that or what.

Throughout each chapter there will be "boxes" labeled "TRY THIS", Look at chapter 9.9.2 and you should find one of them, it starts with: "Run this program with a variety of inputs.". He is trying to insure that you did those exercises, they are usually reinforcing the information in that section.

For problem two did you verify your results?

Why is that function returning a value?

Why is that function printing the result, it would be better to use the value returned and print the value in the calling function. You should strive to have your functions do as little as possible but do it well. In that function you should just compute the Km and return that value to the calling function. And it would be better if that "magic number" was a well named constant.

For exercise 3, what did your compiler say about that program? Do you really understand what your compiler is saying about the problems, this is really what this exercise is all about (learning to understand your compiler).

By the way were are all the #include files, without the #includes you don't have a compilable program. And all of those programs should have a description of the problem being solved.

Exercise 4.

What happens if the variables are equal?

Have you considered taking some elementary math courses? You really shouldn't need to find and example of something as simple as computing ratios, a definition should really be all you might need.

What is the output from all of these functions? Do you really understand why you're getting what you are?

How does the output of Exercise 4 compare to Exercise 5 when you input the same numbers? Do you understand the differences between them?

Exercise 6 still has the issue I mentioned in your other topic, perhaps you should review the last few posts in that topic.

Waiting for exercise 7, should be simple if you understand strings.

Jim

### #5 ndc85430

• I think you'll find it's "Dr"

Reputation: 976
• Posts: 3,849
• Joined: 13-June 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 16 March 2019 - 04:02 AM

albert003, on 12 March 2019 - 09:48 PM, said:

```//converts miles to kilometres
double results(double m);

```

If you have to comment your variables or functions it's probably a sign that the names aren't good. I'm sure we've mentioned this before, but if not: choose meaningful names for the things in your programs. It's important, because good naming helps people to read and understand code. It's not that much effort, either.

### #6 Skydiver

• Code herder

Reputation: 6818
• Posts: 23,194
• Joined: 05-May 12

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 16 March 2019 - 05:41 AM

Actually, it may not be a lot of effort, but it is hard to come up with a name. />

Quote

There are only two hard things in Computer Science: cache invalidation and naming things.

-- Phil Karlton

It's part of the reason why a lot of Windows programmers latched unto Systems Hungarian -- they name things based on the type of the data and eschew the optional descriptor after the type. It start getting harder when you push people towards Office Hungarian where you are supposed to name things based on their purpose with an optional descriptor to distinguish between potential duplicates.

But anyway, with modern IDEs and no more need to do code reviews on paper, there is very little reason to continue to use Hungarian unless you are still doing kernel level debugging with just symbols but no code, or if your team needs to choose a consistent naming convention and nobody will agree on how to name things.

Edit after: My session got closed for reason...
To bring this all back to the advice above: Please use descriptive names. Think about how easy or hard it will be for you to understand the code a year from now, or five years from now. As noted above, if you need to write a comment regarding the variable, perhaps the description you are using in the comment is a more appropriate name.

### #7 albert003

Reputation: 26
• Posts: 677
• Joined: 15-December 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 16 March 2019 - 01:30 PM

Ok, it took me a while because I wanted to make sure I got all of your suggestions. I still need to work on lesson 5 through 7.

I tried to follow one of Jims suggestions and simplify everything. Ill start with..
#2 miles to kilometres.
ndc85430 and skydiver
Thank you for the suggestions. Good point I changed the name of the function so I could know right away what its suppose to do. I have a hard time sometimes thinking of a good name for my functions or classes
```#include<iostream>

double MilesToKm(double m);
int main()
{
double miles;
std::cout << "Convert Miles to Kilometres"
<<"\nEnter the speed in miles:\n" <<std::endl;
std::cin >> miles;
miles = MilesToKm(miles);
std::cout << "That speed is " << miles << " kilometres." << std::endl;
}
double MilesToKm(double m)
{
double km = m * 1.609;

return km;
}

```

//3
This is one of the reasons why it took me so long to get back to you. My IDE code blocks had the compiler error messages hidden and I had to troubleshoot it so I could see the messages.
```#include<iostream>

int main()
{
//illegal
int double = 0;
float age 1;
std::string 365_days;
double 1name;
//legal
std::string name1;
int number;
double customer_change;
}

```

line 35 error: expected unqualified-id before '=' token
meaning that you cannot have a int and a double together = 0. It has to be one or the other.
line 36 error expected initializer before numeric constant
That one means you cannot have an integer value for a float. It has to be 1.0 not 1.
line 37 error expected initializer before numeric constant
You cannot have a numeric value for a string.
line 38 error expected initializer before numeric constant
You cannot have a string for a value for a double.

//4
```#include<iostream>
void EnterNumbers(int &val1,int &val2);
int SizeCompare(int val1,int val2);
int Adding(int val1, int val2);
int Multiply(int val1,int val2);
void EqualRatio(int val1,int val2);
void RatioSize(int val1,int val2);
int Ratios(int val1,int val2);
int main()
{
int val1;
int val2;
int total;
EnterNumbers(val1,val2);
total = SizeCompare(val1,val2);
if(SizeCompare(val1,val2) > 0)
std::cout << total << " is larger" <<std::endl;
else
std::cout << "They are even" <<std::endl;
std::cout << "The sum of the two numbers is " << total << std::endl;
total = Multiply(val1,val2);
std::cout << "Multiplying the two numbers ia: " << total << std::endl;
std::cout <<"Which gives us a ratio of ";
RatioSize(val1,val2);
EqualRatio(val1,val2);

}
void EnterNumbers(int& val1,int& val2)
{
std::cout << "Enter two numbers" <<std::endl;
std::cin >>val1 >> val2;
}
int Ratios(int val1,int val2)
{
if(val2 == 0)
return val1;

return (val2,val1%val2);
}
void RatioSize(int val1,int val2)
{
if(val1 > val2)
{
std::cout << val2<<":"<<val1<<std::endl;
}
else if(val1 < val2)
{
std::cout << val1 <<":"<<val2 <<std::endl;
}
else
std::cout << "Neither is greater" <<std::endl;
}
void EqualRatio(int val1,int val2)
{
int cal,a,b,ans;
ans = Ratios(val1,val2);
a = val1/ans;
b = val2/ans;
std::cout << "The equal ratio is ";
std::cout << a <<":"<<b<<std::endl;
}
int Multiply(int val1, int val2)
{
return val1 * val2;
}
int Adding(int val1, int val2)
{
return val1 + val2;
}
int SizeCompare(int val1,int val2)
{
if(val1 < val2)
return val2;
else if(val1 > val2)
return val1;
else
return 0;
}

```

This is what he wants me to do for exercise 5.

Modify the program above to ask the user to enter floating-point values and store them in double variables. Compare the outputs of the two programs for some inputs of your choice. Are the results the same? Should they be? What's the difference?

I re read exercise 5 and I'm confused. Am I suppose to use float types with double functions?. I might be wrong but I didn't think you could use float types in a double function.

I'll work on the rest of the exercises and post them once I finish them.

### #8 jimblumberg

Reputation: 5702
• Posts: 17,481
• Joined: 25-December 09

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 16 March 2019 - 06:17 PM

Quote

```    int double = 0;
```

line 35 error: expected unqualified-id before '=' token
meaning that you cannot have a int and a double together = 0. It has to be one or the other.

No, in this case it means that "double" is a reserved word and can't be used as a variable name.

Quote

```    float age 1;
```

line 36 error expected initializer before numeric constant
That one means you cannot have an integer value for a float. It has to be 1.0 not 1.

No, this means that you need to use either the assignment operator= or initialize the variable using some other method: float age(1);.

Quote

```    std::string 365_days;

```

line 37 error expected initializer before numeric constant
You cannot have a numeric value for a string.

No, this actually means that a variable name cannot start with digits.

Quote

```    double 1name;
```

line 38 error expected initializer before numeric constant
You cannot have a string for a value for a double.

Again, no, it really means that a variable name cannot start with a digit.

Maybe you need to reread the chapter, especially section 3.7.

Okay now to Exercise 4:

Quote

Write a program that prompts the user to enter two integer values. Store these values in int variables named val1 and val2. Write your program to determine the smallest, largest, sum, difference, product, and ratio of these values and report them to the user.

To me you have over complicated things (big time). IMO, this program would probably be easier without all the functions but with your current code you have several problems. First your "enter" function is not properly prompting the user for two integer values, you're currently just asking for two numbers which could be any combination of integer or floating point values.

Next your SizeCompare() function doesn't make much sense. Look at how you're calling that function.

```    total = SizeCompare(val1,val2);
if(SizeCompare(val1,val2) > 0)
std::cout << total << " is larger" <<std::endl;
else
std::cout << "They are even" <<std::endl;
```

First why are you calling it twice with the same values? And that function name is horrible.

Second where are you reporting to the user which number is the smallest and which number is the largest? All I see is larger and even but no mention of smallest.

Next most of the rest of your function names are poor, IMO. Your instructions say to determine the sum, difference, product, and ratio, why not use these terms for your functions?

Now for the ratio, why are you playing with how you're "building" your ratio? If you enter two numbers, say 1 and 3 your ration is 1:3, if you enter 3 and 1 your ratio should be 3:1, you may want to "simplify" the ratio, ie if you entered 3 and 6 which is 3:6 or 1:3. Perhaps this link might be of some use. I believe that the author expects part of the answer to be the first number divided by the second number, ie: 1:3 should show the value of 1/3.

Quote

I re read exercise 5 and I'm confused. Am I suppose to use float types with double functions?.

No, when talking about floating-point type you need to realize that there are at least two distinct floating-point types, the type float, and the type double.

Jim

### #9 ndc85430

• I think you'll find it's "Dr"

Reputation: 976
• Posts: 3,849
• Joined: 13-June 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 17 March 2019 - 02:39 AM

albert003, on 16 March 2019 - 08:30 PM, said:

```int Multiply(int val1, int val2)
{
return val1 * val2;
}
int Adding(int val1, int val2)
{
return val1 + val2;
}

```

Sometimes, you have to decide when not to write more code. What is the value in having these two functions? You're not abstracting away some relatively complex procedure for ease of reading or reuse - those two expressions would be perfectly understandable in the places you're calling those functions.

Too much abstraction can be a bad thing. As part of our recruitment process, we send people a technical exercise to do. The application we get them to write isn't that complicated, but one submission I saw recently suffered from too much abstraction. There were several thousand lines of code there and lots and lots of classes and interfaces, often with confusing names but the overall architecture was very difficult to understand because of all these parts.

This post has been edited by ndc85430: 17 March 2019 - 04:15 AM

### #10 andrewsw

• quantum multiprover

Reputation: 6775
• Posts: 27,938
• Joined: 12-December 12

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 18 March 2019 - 01:57 AM

Returning to post #7 and naming:

```    miles = MilesToKm(miles);
std::cout << "That speed is " << miles << " kilometres." << std::endl;
```

miles refers to kilometres, not miles.

```    total = SizeCompare(val1,val2);

```

Sorry to labour the points already made further, but total neither represents a size comparison nor a calculation. There is no reason to attempt to reuse an existing variable for different purposes: it is a bad idea, in any case. Creating different variables should at least push you towards more meaningful names.

### #11 albert003

Reputation: 26
• Posts: 677
• Joined: 15-December 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 18 March 2019 - 07:56 PM

I have a few questions for you guys.

Jim,
You're right, it is a mess. The author wanted me to write a program which will determine the smallest, largest,sum, difference, product, and ratio of these values and report
them to the user. At the time I thought he only wanted me to print the larger of the two numbers inputted. Now I see he wants me to print which is the larger of the two and which is the smaller of the two.

I changed the program and got rid of the function enter as well as renamed the functions to what they do.

What did you mean by this program would probably be easier without all the functions?. I used a function for each thing he wanted me to do in the program. Right now, I have simplified the functions and renamed them to what they will do in the program. These are the names of the functions.
int Smallest(int val1,int val2);
int Largest(int val1,int val2);
int Adding(int val1, int val2);
int Product(int val1, int val2);
void Ratios(int val1, int val2);

Thank you for the video, when you sent me the link to the previous video I thought you wanted me to scale the ratio down. I changed it, simplified it so that if a user enters the numbers 5 and 16 it will say the ratio of the two numbers is 5:16.

Ah ok, so he wants me to make the program using the type float.

ndc85430 I think I understand what you meant by too much abstraction. I cleaned up the program and reduced it to 5 functions, simplifying the program.

I have those two functions because I can't have a single function doing various mathematics. The book I am using said its best to have a function do a single thing,

andrewsw I stared and re read what you said about miles refers to kilometres, not miles. I think I know what you meant.

```double MilesToKm(double m);
int main()
{
double miles;
std::cout << "Convert Miles to Kilometres"
<<"\nEnter the speed in miles:" <<std::endl;
std::cin >> miles;
std::cout << "That speed is " << answer << " kilometres." << std::endl;
}
double MilesToKm(double m)
{
double km = m * 1.609;

return km;
}
```

Thank you for that advice, I changed the variable so that each function had its own variable type and I made it similar to the function it corresponded so I wouldn't be confused when I return to the program later.
For example these are the variable types I use now...
```int bignum,smallnum,Addnum,Multinum,Ratiosnum;
```

This is the program.

```#include<iostream>
/*
Write your program to determine the smallest, largest,
sum, difference, product, and ratio of these values and report
them to the user.
*/

int Smallest(int val1,int val2);
int Largest(int val1,int val2);
int Adding(int val1, int val2);
int Product(int val1, int val2);
void Ratios(int val1, int val2);

int main()
{
int val1;
int val2;
std::cout << "Enter two numbers" <<std::endl;
std::cin >> val1 >> val2;
smallnum = Smallest(val1,val2);
std::cout << smallnum << " is smaller" <<std::endl;
bignum = Largest(val1,val2);
std::cout << bignum << " is larger" <<std::endl;
std::cout << val1 << " + " << val2 <<" equals ";
std::cout << Addnum << std::endl;
Multinum = Product(val1,val2);
std::cout << val1 << " * " << val2 << " equals ";
std::cout << Multinum <<std::endl;
std::cout << "The ratio of the two numbers is " <<std::endl;
Ratios(val1,val2);
}
int Smallest(int val1,int val2)
{
if(val1 > val2)
return val2;
else if(val1 < val2)
return val1;
}
int Largest(int val1,int val2)
{
if(val1 < val2)
return val2;
else if(val1 > val2)
return val1;
}
int Adding(int val1,int val2)
{
return val1 + val2;
}
int Product(int val1, int val2)
{
return val1 * val2;
}
void Ratios(int val1, int val2)
{
std::cout << val1 <<":"<<val2<<std::endl;
}

```

### #12 jimblumberg

Reputation: 5702
• Posts: 17,481
• Joined: 25-December 09

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 18 March 2019 - 08:56 PM

Look at these two functions:

```int Smallest(int val1,int val2)
{
if(val1 > val2)
return val2;
else if(val1 < val2)
return val1;
}
int Largest(int val1,int val2)
{
if(val1 < val2)
return val2;
else if(val1 > val2)
return val1;
}
```

First do you see the code duplication? Isn't there a way you can reuse one of the functions to do both features.

Second what happens if the values are equal? Plus if your compiler is properly setup you should be getting warnings for each of these functions.

Third, why the else if(), since you have multiple return statements you probably don't even need an else statement (plus this would solve the problems pointed out above.

Quote

What did you mean by this program would probably be easier without all the functions?

I mean that you're probably over thinking things again, KEEP THINGS SIMPLE!!!! You should be using simple control structures which probably means that almost all the logic could be in main().

For example the two functions above could be in main() something like:
```// Find the smallest of two numbers.
int smallest;
if(val1 < val2)
smallest = val1;
else
smallest = val2;

// Find the largest of the two numbers.
int largest;
if(val1 > val2)
largest = val1;
else
largest = val2;

```

Note: I'll leave it up to you to see if I got the logic correct. Be sure to test it for equal as well.

The other "functions" could very well be in main as well since they are very simple calculations.

Quote

Thank you for the video, when you sent me the link to the previous video I thought you wanted me to scale the ratio down. I changed it, simplified it so that if a user enters the numbers 5 and 16 it will say the ratio of the two numbers is 5:16.

I hope you actually read the entire page as well, not just watched video. The text has important information as well.

By the way did you miss this from my previous post?

Quote

I believe that the author expects part of the answer to be the first number divided by the second number, ie: 1:3 should show the value of 1/3.

This information is in the text part of that link.

And lastly for now, look at this snippet:
```...
int main()
{
int val1;
int val2;
...

```

This is considered a poor practice in C++, can you tell me why?

Jim

### #13 albert003

Reputation: 26
• Posts: 677
• Joined: 15-December 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 19 March 2019 - 08:01 PM

I changed the program and got rid of the functions. I simplified the smallest, largest.

The reason why its poor practice is because having the variable types next to one another makes them harder to read. When theyre on top of each other also theyre easier to find.

I've also been working on the ascending numbers using your example of two functions.

This is the program.
```#include<iostream>

int main()
{
int val1;
int val2;
int largest;
int smallest;
int product;
int ratios;
std::cout << "Enter two numbers" <<std::endl;
std::cin >> val1 >> val2;

if(val1 < val2)
{
largest = val2;
smallest = val1;
std::cout <<"The smaller number is "<< smallest<<std::endl;
std::cout <<"The larger number is "<< largest<< std::endl;
}
else if(val1 > val2)
{
largest = val1;
smallest = val2;
std::cout <<"The smaller number is "<< smallest<<std::endl;
std::cout <<"The larger number is "<< largest<< std::endl;
}
else
{
std::cout <<"They are the same " <<std::endl;
}
adding = val1 + val2;
std::cout << val1 << " plus " << val2 << " equals " <<adding << std::endl;
product = val1 * val2;
std::cout << val1 << " times " << val2 << " equals "<< product << std::endl;
std::cout <<"The ratio of the two numbers is "<<std::endl;
std::cout <<val1<<"/"<<val2<<std::endl;
}

```

### #14 jimblumberg

Reputation: 5702
• Posts: 17,481
• Joined: 25-December 09

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 19 March 2019 - 08:46 PM

Quote

The reason why its poor practice is because having the variable types next to one another makes them harder to read. When theyre on top of each other also theyre easier to find.

That's only a part of the answer. The biggest reason that it is considered a poor practice is because you're not declaring your variables close to first use. It is considered a poor practice in C++, and also modern C, to declare all of your variables in one big chunk at the beginning of scopes.

```    if(val1 < val2)
{
largest = val2;
smallest = val1;
std::cout <<"The smaller number is "<< smallest<<std::endl;
std::cout <<"The larger number is "<< largest<< std::endl;
}
else if(val1 > val2)
{
largest = val1;
smallest = val2;
std::cout <<"The smaller number is "<< smallest<<std::endl;
std::cout <<"The larger number is "<< largest<< std::endl;
}
else
{
std::cout <<"They are the same " <<std::endl;
}
```

Again why all the code duplication? Those print statements should only be needed once. And really, for this exercise, you really don't need to worry about telling the user that they are the same since that is not part of the exercise. Just tell the user the value of the smallest and largest, but know that you could have the same number printed twice.

Jim

### #15 albert003

Reputation: 26
• Posts: 677
• Joined: 15-December 14

## Re: Exercises 1 - 6 Principal & practice using C++

Posted 20 March 2019 - 10:53 AM

That actually makes sense. That way the type variable is just above what it was intended to do. Which explains why in the book the author did that in his programs. I always thought he did that so the person learning could see the variable type just above what it was suppose to be used for in the program.

Good point. I just need to have the string saying which of the two numbers is larger and smaller once and outside of the if statements. I found a good article on ternary operators so I replaced the if and else if statements then replaced it with the ternary operator to cut lines of code.

This is the program
```#include<iostream>

int main()
{
int val1;
int val2;
std::cout << "Enter two numbers" <<std::endl;
std::cin >> val1 >> val2;
int largest;
int smallest;
largest = val1 > val2 ? val1 : val2;
smallest = val2 < val1 ? val2 : val1;
std::cout <<"The smaller number is "<< smallest<<std::endl;
std::cout <<"The larger number is "<< largest<< std::endl;
adding = val1 + val2;
std::cout << val1 << " plus " << val2 << " equals " <<adding << std::endl;
int product;
product = val1 * val2;
std::cout << val1 << " times " << val2 << " equals "<< product << std::endl;
std::cout <<"The ratio of the two numbers is "<<std::endl;
std::cout <<val1<<"/"<<val2<<std::endl;
}

```