C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

print three lists of your top five customers. The outer loop would move from 1 to 3 , while the inner
loop would print the top five customers.


Here’s a full program that executes a for loop based on the number of movies a user has claimed to
see in the current year. It asks for the name of the movie and a rating on a scale of 1 to 10. It then tells
the user what movie was ranked as a favorite and what movie was the least favorite:


Click here to view code image


// Example program #1 from Chapter 15 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter15ex1.c
/* This program will ask users how many movies they've seen this
year, and then loop through asking the name of each movie and a
rating from 1 to 10. It will remember their favorite movie and their
least favorite movie. */
#include <stdio.h>
#include <string.h>
main()
{
int ctr, numMovies, rating, favRating, leastRating;
char movieName[40], favorite[40], least[40];
//initialize the favRating to 0 so any movie with any rating of
// 1 or higher will replace it and the leastRating to 10 so any
// movie rated 9 or lower will replace it
favRating = 0;
leastRating = 10;
// Find out how many movies the user has seen and can rate
// The loop will continue until they enter a number more than 0
do {
printf("How many movies have you seen this year? ");
scanf(" %d", &numMovies);
// If the user enters 0 or a negative number, the program
// will remind them to enter a positive number and prompt
// them again
if (numMovies < 1)
{
printf("No movies! How can you rank them?\nTry again!\
n\n");
}
} while (numMovies < 1);
for (ctr = 1; ctr <= numMovies; ctr++)
{
//Get the name of the movie and the user's rating
printf("\nWhat was the name of the movie? ");
printf("(1-word titles only!) ");
Free download pdf