[C language] Operations, Expressions, and Operators (Chapter 5)


The fifth chapter in Stephen Prata’s C Primer Plus. It deals with statements and the while loop. Curiously, in Prat’s book, while comes before if/else in terms of presentation; while in programming, loops are often taught already after conditional statements.

In addition to solutions, I will post my versions of some of the (most curious) programs from the listings. I do this – I look through the listing, then I close the book and write “in my own words”:

1) Create a table of squares of numbers
#include <stdio.h>
int main (void)
{
int n=1;
int sqrt;
while (n<10)
{
sqrt=n*n;
printf("Square root for %d is %d\n",n,sqrt);
n=n+1;
}
getchar();
return 0;
}

2) Program about grains on a chessboard ← link; posted as a separate article on the site.

3) Converting seconds to minutes and seconds (modulo: %)

#include <stdio.h>
int main(void)
#define SEC_TO_MIN 60
{
int sec, min, sec_left;
printf("Enter second to transfer them to minites:\n");
scanf("%d",&sec);
min = sec / SEC_TO_MIN;
sec_left = sec % SEC_TO_MIN;
printf("%d seconds in minutes: %d minites %d seconds", sec, min, sec_left);
getchar();getchar();
return 0;
}

This is the first version of the program. Now we will make it so that it periodically requests the input of new values:

#include <stdio.h>
int main(void)
#define SEC_TO_MIN 60
{
int sec, min, sec_left;
printf("Enter second to transfer them to minites:\n(to finish enter 0)\n");
while (sec>0)
{
scanf("%d",&sec);
min = sec / SEC_TO_MIN;
sec_left = sec % SEC_TO_MIN;
printf("%d seconds in minutes: %d minites %d seconds\n", sec, min, sec_left);
}
getchar();
return 0;
}

4) Problem with the program from the paragraph “Decrease operation”
(Listing 5-12 bottles.c program)

#include <stdio.h>
#define MAX 100
int main(void)
{
int count = MAX + 1;
while (--count > 0)
{
printf("%d bottles in box, "
"%d bottles in box\n", count,count);
printf("Drink one... and now there are %d bottles in the box\n\n", count - 1);
}
getchar();
return 0;
}

Here’s a funny thing. The output shows the beginning with “Drink one… and now there are 99 bottles in the box”. It turned out that the length of the Windows console is not enough to put all the values when you run the program from the compiler. If you make output to a file (batch file with the contents: project1.exe >> a.txt), then the first lines fit and the output is obtained as in a book.

And another funny thing – in my book of 2000 there are such phrases:

100 бутылок пива на полке, 100 бутылок пива!
Сними одну и пусти ее по кругу,
99 бутылок пива на полке, 99 бутылок пива!..

Well, in the book of 2014, beer was changed to spring water .. Where is the world heading?! 🙂


The rule of using increment operators ++ and decrement —

  • Do not use an increment or decrement operation on a variable that is part of more than one function argument.
  • Do not use the increment or decrement operation on a variable that
    appears more than once in an expression.

Task from “Control questions”:

You need to change this program:

#include <stdio.h>
#define TEN 10
int main(void)
{
int n = 0;
while (n++ < TEN)
        printf("%5d", n);
printf("\n");
getchar(); getchar();
return 0;
}

… which outputs:

    1    2    3    4    5    6    7    8    9   10

…so that it prints characters from a to g.

My first option:

/// a to g
#include <stdio.h>
#define G 103 // 103 is ANSI for g
int main (void)
{
int n = 96; // 96 is ANSI for ` (just before 'a')
while (n++ < G)
        printf("%5c", n);
printf("\n");
getchar(); getchar();
return 0;
}

Second option:

#include <stdio.h>
#define LIMIT 'h'
int main(void)
{
char n = 'a';
while (n < LIMIT)
        {
        printf("%5c", n);
        n++;
        }
getchar();
return 0;
}

In this variant, you have to use h, because the book has not reached <= yet, and you need to cope with the tasks with the supplied tools 🙂 Having looked at the solution of this program in the book, I see that I did not guess before the increment was transferred to the printf function; here it is:

#include <stdio.h>
int main(void)
{
char ch = 'a';
while (ch <= 'g')
        printf("%5c", ch++);
printf("\n");
getchar();
return 0;
}

It’s beautiful, you can’t say anything 🙂 Although <= this is cheating at this stage of the book.

So we finally get to the programming exercises at the end of Chapter 5:

1) Write a program that converts time in minutes to hours and minutes. For the value 60, create a symbolic constant with #define or const. Use a while loop to allow the user to re-enter values and to terminate the loop if a time value less than or equal to zero is entered.

#include <stdio.h>
#define MIN_PER_HOUR 60
int main (void)
{
int min, hour, min_left;
printf("Enter time in minutes\n");
scanf("%d", &min);
while (min > 0)
        {
        hour = min / MIN_PER_HOUR;
        min_left = min % MIN_PER_HOUR;
        printf("%d minutes is: %d hours, %d minutes\n\n",
        min, hour, min_left);
        printf("Enter more minutes to convert or 0 to quit:\n");
        scanf("%d", &min);
        }
getchar();
return 0;
}

2) Write a program that asks the user to enter an integer and then prints out all integers from (and including) that number to the number that is greater than (and including) 10. (That is, if the number 5 is entered, then the numbers from 5 to 15 should be present in the output). Ensure output values are separated from each other by spaces, tabs, or newlines.

#include <stdio.h>
int main (void)
{
int n=0, z=0;
printf("Choose the number, Neo\n");
scanf("%d", &n);
z = n;
while (n <= z+10)
        {
        printf("%d\n", n++);
        }
getchar();getchar();
return 0;
}

And the second approach:

#include <stdio.h>
int main(void)
{
int n = 0, z = 0;
printf("Choose the number, Neo\n");
scanf("%d", &n);
while (z <= 10)
	{
	printf("%4d\n", n + z);
	z++;
	}
getchar();getchar();
return 0;
}

3) Write a program that asks the user to enter a quantity
days and then converts that value to a number of weeks and days. For example,
18 days the program should convert to 2 weeks and 4 days. Display results in the following format:
18 days is 2 weeks and 4 days.
To allow the user to repeatedly enter the number of days, use
while loop. The loop should terminate when the user enters a non-positive value, such as 0 or -20.

#include <stdio.h>
#define DAYS_PER_WEEK 7
int main(void)
{
int day, week, day_left;
printf("Enter happy days\n");
scanf("%d", &day);
while (day > 0)
        {
        week = day / DAYS_PER_WEEK;
        day_left = day % DAYS_PER_WEEK;
        printf("%d days is: %d weeks %d days\n",
        day, week, day_left);
        printf("Enter another value or 0 to exit\n");
        scanf("%d", &day);
        }
getchar();
return 0;
}

4) Write a program that prompts the user for the height in centimeters and then displays the height in centimeters as well as feet and inches. Fractional parts of centimeters and inches must be allowed. The program should allow the user to continue entering height values ​​until a non-positive value is entered. The output of this program should look like this:
Enter height in centimeters: 182
182.0 cm = 5 feet, 11.7 inches
Enter the height in centimeters (<=0 to exit the program): 168.7
168.0 cm = 5 feet, 6.4 inches
Enter the height in centimeters (<=0 to exit the program): 0
Work completed.

Faced a problem – it is impossible to bring fractional values ​​to the module. How in this case to find inches with the required accuracy? I had to dance with a tambourine:

#include <stdio.h>
#define CM_PER_INCH 2.54
#define INCH_PER_FEET 12.0
int main(void)
{
float cm, feet, inches, iches_left, inches_float;
printf("Enter you height in centimeters:\n");
scanf("%f",&cm);
while (cm > 0)
{
inches = cm / CM_PER_INCH;
iches_left = (int)inches % (int)INCH_PER_FEET; // modulus operator fail to work with floating values
feet = (inches - iches_left) / INCH_PER_FEET;
inches_float = inches - ((int)feet * INCH_PER_FEET);
printf("%.1f cm is %.0f feets %.1f inches\n",cm,feet,inches_float);
printf("Enter another one or 0 to exit\n");
scanf("%f",&cm);
}
getchar();
return 0;
}

There is a more elegant solution if you forget about the module and use type casting correctly (see comments):

#include <stdio.h> #include <stdio.h>
#define CM_PER_INCH 2.54
#define INCH_PER_FEET 12.0
int main(void)
{
float cm, inches, inches_left;
int feet;
printf("Enter you height in centimeters:\n");
scanf("%f",&cm);
while (cm > 0)
{
inches = cm / CM_PER_INCH;
feet = inches / INCH_PER_FEET; // floating-point value got truncated because 'int feet'
inches_left = inches - (feet * INCH_PER_FEET); // as operation involves two types - 'float inches' and 'int feet' - both values 'promoted' to higher ranking and become floating-point
printf("%.1f cm is %d feets %.1f inches\n",cm,feet,inches_left);
printf("Enter another one or 0 to exit\n");
scanf("%f",&cm);
}
getchar();
return 0;
}

5) Modify the addemup.c program (listing 5.13), which calculates
the sum of the first 20 integers. (If you like, you can read addemup. with a program that calculates the amount you will have after 20 days, if you get $1 on the first day, $2 on the second day, $3 on the third day, and so on.) Modify the program so that you can interactively specify how far the calculation should extend. In other words, replace the number 20 with a variable whose value is entered by the user.

// summ of first n values
#include <stdio.h>
int main(void)
{
int n=0, z=0, x;
printf("Enter number of integers to sum\n");
scanf("%d", &x);
while (n++<x)
z=n+z;
printf("%d\n", z);
getchar();getchar();
return 0;
}

6) Now modify the program in Exercise 5 to calculate the sum of the squares of integers. (Or, if you prefer, the program should calculate the amount you would get if you were paid $1 on the first day, $4 on the second day, $9 on the third day, and so on.) square, but as you know, the square of n is n*n.

// summ of squares
#include <stdio.h>
int main(void)
{
int n=0, z=0, x;
printf("Enter number of squares to sum\n");
scanf("%d", &x);
while (n++<x)
         z=(n*n+z);
printf("%d\n", z);
getchar();getchar();
return 0;
}

7) Write a program that asks for a double number and prints the value of the cube of that number. To do this, use your own function that cubes the value and prints the result. The main() program must pass the input value to this function.

In this example, I ran into a gag – the program gave out zeros. It turned out that this is due to passing the value to scanf(“%f”,…), while the calculations are carried out with double precision.

This happens because the function does not know what types the parameters passed to it have, because parameters are not passed, but pointers. Therefore, the function needs to know exactly what type the received pointer points to – a float (32 bits) or a double (64 bits).

This means that the function does not understand which bit the decimal separator is in and therefore returns wild. While in C it converts float to double for function arguments – references (pointer) are not converted in any way (I hope I explained it clearly .. I chewed it for myself as well). So the program:

#include <stdio.h>
void cube(double n);
int main(void)
{
double n;
printf("Enter number to cube it\n");
scanf("%lf", &n);
cube(n);
getchar();getchar();
return 0;
}
void cube(double n)
{
double x3 = n*n*n;
printf("Cube of %f is %f",n, x3);
}

8) Write a program that prints the results of applying the modulo operation. The user must first enter an integer value,
which is used as the second operand and remains unchanged.
Then the user must enter the numbers for which the modulo result will be calculated. The process should be interrupted by entering a value that is equal to or less than 0. An example execution of this program should look like this:
This program calculates the results of modulo division.
Enter an integer to serve as the second operand: 256
Now enter the first operand: 438
438% 256 equals 182
Enter the next number for the first operand (<= 0 to exit
programs): 1234567
1234567 % 256 equals 135
Enter the next number for the first operand (<= 0 to exit
programs): 0
Ready

#include <stdio.h>
int main(void)
{
int x, y;
printf("Enter second modulus operator\n");
scanf("%d",&y);
printf("Enter first modulus operator\n");
scanf("%d",&x);
while(x > 0)
  {
  printf("%d %% %d = %d\n", x, y, x%y);
  printf("Enter another first modulus operator or 0 for exit\n");
  scanf("%d",&x);
  }
getchar();
return 0;
}

9) Write a program that prompts the user to enter a Fahrenheit temperature value. The program must read the temperature value as a double and pass it as an argument to a user-defined function named Temperatures(). This function should calculate the Celsius and Kelvin equivalent temperatures and display all three temperatures on the screen to two positions to the right of the decimal point. The function must identify each value with the corresponding temperature scale symbol. Here is the formula for converting Fahrenheit temperature to Celsius temperature:
Celsius temperature = 5.0 / 9.0 * (Fahrenheit temperature – 32.0)
In the Kelvin scale, which is commonly used in science, 0 represents absolute zero, i.e. minimum temperature limit. The formula for converting temperature from Celsius to Fahrenheit is:
Kelvin temperature = Celsius temperature + 273.16
The Temperatures() function must use const to create symbolic representations of the three constants that are involved in the conversions. To
to give the user the ability to repeatedly enter temperature values, a loop should be organized in the main () function that ends when the character q or another non-numeric value is entered. Take advantage of the fact that the scanf() function returns the number of items it read, so it will return 1 if it reads a number, but will not return 1 when the user enters q. The == operator performs an equality test, so it can be used to compare the return value of scanf() with 1.

// to check: 32 F is 0 C and 273.16 K
#include <stdio.h>
void temperatures (double f);
int main(void)
{
double fahrenheit;
printf("Enter temperature in F: ");
while (scanf("%lf", &fahrenheit) == 1)
{
temperatures(fahrenheit);
printf("Enter another F-value to calculate or any letter-symbol to exit\n");
}
getchar();getchar();
return 0;
}
void temperatures (double fahrenheit)
{
const double a = 5.0; // F to C
const double b = 9.0; // F to C
const double c = 32.0; // F to C
const double d = 273.16; // F to K
double celsium = a / b * (fahrenheit - c);
double kelvin = celsium + d;
printf("%.2f fahrenheit is %.2f C or %.2f K\n",fahrenheit, celsium, kelvin);
}

I will be glad to see your comments!


This entry was posted in C language (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.