Input / Output
Simple Program
Difficulty: Easy- Write a program that asks the user for 10 integers, computes their sum, and displays it at the end (using strictly fewer than 10 variables).
- Modify the previous program so that it displays intermediate sums.
- Modify the previous program to display the average of these 10 integers.
- Modify the previous program to display the average of strictly positive integers.
Simple Game
Difficulty: EasyWrite a program that allows a player (whose name you will ask for) to try to determine a number between 1 and 100. This number will be chosen randomly (see end of exercise) and you must guide the player by indicating whether their guess is lower or higher than the number to find. Display the number of attempts taken to find the correct number.
#include <stdio.h> // utilisation de printf et scanf
#include <stdlib.h> // utilisation de srand et rand
#include <time.h> // utilisation d'une horloge
int main()
{
// initialisation du generateur de nombre aléatoire
// en se basant sur une graine qui doit changer à chaque execution
// on utilise donc la date / heure courante
srand(time(NULL));
// rand donne une valeur comprise entre 0 et RAND_MAX
// on prend une valeur aléatoire, on en calcule le mod par 100
// ce qui donne une valeur comprise entre 0 et 99
// on ajoute 1 pour avoir une valeur entre 1 et 100
int random_int = rand() % 100 + 1 ;
// à vous de jouer ...
return 0;
}
Countdown
Difficulty: EasyWrite an algorithm that, given nbH, nbM, nbS denoting respectively a number of hours, minutes, seconds, counts down this duration (displaying every tuple (h,m,s)) and displays TIME! at the end of the countdown.
EOF and Redirections
Difficulty: EasyWrite a program that asks the user for a series of integers (no limit on the number) and displays the sum of these integers, the average, and the extremal values. Input will end when the user presses CTRL+D (see below how to handle this combination in C), and you will display the various computed values at that point.
Then use redirections to read integer values from a text file you will have created.
#include <stdio.h>
int main()
{
int value;
while ( EOF != scanf("%d", &value) )
{
// a vous de jouer...
}
return 0;
}
Redirections
Difficulty: EasyBased on the previous exercise, write a copy program that allows duplicating a file. The usage syntax will be for example copy < source_file > destination_file.