DOC

# These are optional problems

By Mark Sims,2014-06-14 22:05
14 views 0
These are optional problems

Jim’s

C Programming Problems

The First 100

(currently up to 74)

gcc ansi -Wall

A Varied Collection of Problems for Beginning Students

Version 1.0

Last Updated January 3, 2008

Problem 1: Hello World

Write a C program that prints out the message Hello World.

Problem 2: The Value 6

Write a C program that prints out the value 6. I want you to use the %d format code.

Problem 3: The Character P

Write a C program that prints out the single character P. I want you to use the %c format code.

Problem 4: The scanf Function

Write a C program that reads in an integer value from the keyboard via the scanf function

and then prints it back onto the screen. By now you should know that scanf seeks an address expression. For example, &n is an address expression.

Problem 5: Sum of Two Values

Write a C program that reads two integer values from the keyboard via the scanf function,

then adds them together, stores the result into a variable called sum, and then prints out the value of the variable sum.

Problem 6: The fscanf Function

Create file called testdata6 that has a single integer value stored into it.

Write a C program that opens a file named testdata6, reads the single value stored in it

using the fscanf function, and then prints the value back onto the screen. You should call the function fclose before you return to the Unix operating system.

Problem 7: Bigger than 100?

Write a C program that reads a number from the keyboard via the scanf function, and then prints the message The number is bigger than 100 if it is. If the number is 100 or less

than 100 then you should print out the message The number is not bigger than 100.

Problem 8: One Line of Asterisks

Create a file called testdata8 that has a single integer value stored into it. The integer will be a number between 1 and 30 inclusive. Create a C program that uses the fscanf function to read in this integer value and then prints a line containing that many asterisks’ (*). Use a for-loop in your program.

Problem 9: Using a For Loop

A file called testdata9 contains 5 positive integer values. Write a C program that reads and prints all five values. Each number should be read using the fscanf function embedded inside a for-loop. You should not use an array. You should a for loop.

Problem 10: Sum of Five

Write a C program that reads in 5 positive integer values from a file called testdata10 and

computes their sum, and then prints the value of the sum onto the screen.

Problem 11: Equal to Zero?

Write a C program that reads a single integer number from the keyboard via the scanf function, determines if the number is equal to zero or not, and then either prints out: “The

number is equal to zero.” or “The number is not equal to zero.”

Problem 12: Positive, Negative, or Zero?

Write a C program that reads a single integer number from the keyboard via the scanf function, determines if the number is either, positive, zero, or negative and then prints out a message such as: “The number is positive.” or “The number is zero.” or “The number is

negative.”

Problem 13: The abs Function

Write a C program that reads a single integer value from the keyboard. The number could be either a negative number, a positive number, or zero. Your program should call the absolute value function abs and then print out the absolute value of number entered. You can browse appendix D to determine which header file you should include. Your textbook is your best friend.

Problem 14: Argc

Write a C program that prints the number of command line arguments. This is the number that is stored in the argument: argc.

Problem 15: Using the sqrt Function

Write a C program that asks the user to enter a floating point number from the keyboard and then prints out the square root of that number. You should call the sqrt function. You can browse Appendix D to which header file you should include. The textbook is your best friend. Read it. You should know that the sqrt function definition is not located in libc.a. It is located in libm.a and thus you will need to use the lm option when you try to

build the executable image via the gcc utility. You also need to include math.h.

Problem 16: Area of a Rectangle

In grade school you learned that the area of a rectangle is its length times its height. Write a C program that inputs two floating point numbers that represents the length and the height of a rectangle. Your program should output the area of the rectangle.

Problem 17: Area of a Circle

In grade school you learned that the area of a circle is pi * r squared. Write a C program program that inputs a single floating point number representing the value of the radius r. Your program should output the area of the circle with a radius r. Now, the only problem is what value should you use for pi? Hmmm. Well, inside of math.h is a the following line:

#define M_PI 3.14159265358979323846 /* pi */

so you need to include math.h and then use M_PI in an expression to calculate the area. I do not want you to type in this #define. I want you to include . However, with the ansi option you will need to add another option otherwise things won’t go too well.

You do not need to compile the program with the lm option since you are not missing

any definitions.

Program 18: Argv

Write a C program that will print out each command line argument on a separate line. Use a for-loop. The loop should have an index variable i that ranges from 0 to (argc-1).

Remember the first command line argument is called argv[0] and its type is string so you can print it out with a %s format code. You should also recall that the character combination \n means newline.

Program 19: Reverse the Command Line

Write a C program that will print out each command line argument on a separate line

however you should print them out in reverse order. That is, the last command line argument is printed first.

Program 20: Scanf Returns What?

Create a file and call it testdata20. The file should contain an unknown number of integer

values. Write a C program that uses a while loop to read in each number and then prints the number back onto the screen. The Boolean expression of the while loop should contain a call to the fscanf function.

Program 21: One Dimensional Array

Create a file and call it testdata21. The file should contain exactly 15 integer values.

Read these 15 numbers into a one-dimensional array. Then print the numbers out in the reverse order from which they were entered.

Program 22: Sum of a Bunch

Create a file and call it testdata22. The file should contain an unknown number of integer

values. Write a program that will read in these numbers until end of file is reached and then outputs the sum of the numbers. You should not use an array to do this problem.

Program 23: fgetc and toupper

Write a program that reads in a text file character by character using fgetc. The name of the textfile is testdata23. Your program should print out an exact copy of the file it read in except that all lowercase letters are converted to uppercase letters. You may use the function toupper (see appendix D). You should also use the function putchar.

Program 24: Reverse

Write a program that reads in 10 integer numbers from the keyboard using scanf. Your program should then print out the numbers in reverse order. Your program should store the 10 numbers into a one-dimensional array. Remember, scanf wants you to provide it an address expression. Do you see that this problem requires you to “hold” all the

numbers at once before you can generate the answer? This was not the case when we computed the average of some numbers.

Program 25: Digit Sum

A file contains a bunch of positive integers. Write a program that reads in the numbers, one by one, and then outputs the sum of the individual digits. For example, if the number 6115 is read in, then your program would output 13. Your main program must call a function that receives an integer argument and returns the sum. This function should not print out the answer, it should only compute and return the answer. The main function will print out the answer.

Program 26: Bubble Sort

A file contains 10 integers. Write a program to read these numbers and then sort them using a standard bubble sort. Your program should output the numbers sorting into 2ascending order. You should realize that the bubble sort is on Order N comparison based

sorting strategy. The name of the file is to be passed to your program as argv[1].

Program 27: Inner Product of Two Vectors

Write a program that will input two vectors of information. Each vector will contain exactly 8 floating point values. Your program will enter these values in from the standard

input. Once the values have been read in, your program should call a function that will compute the inner product of these two vectors. The function should return the value back to the main program. The main program will then print out the value. You should not print the value out inside the inner product function.

The inner product of two vectors is a single number obtained by multiplying corresponding elements and then adding up their sums. For example, if vector u = (5, 1, 6, 2) and vector v = (1, 2, 3, 4) then the inner product is 33 because

(5 * 1) = 5

(1 * 2) = 2

(6 * 3) = 18

(2 * 4) = 8

and 5 + 2 + 18 + 8 is 33.

Your C program will call a function called inner that has the following interface:

float inner ( float u[] , float v[], int size ) ;

Program 28: Persistence of a Number

Multiplying the digits of an integer and continuing the process gives the surprising result that the sequence of products always arrives at a single digit number. For example,

715 ---- 35 ---- 15 ---- 5

27 ---- 14 ---- 4

4000 ---- 0

9

The number of times products need to be calculated to reach a single digit is called the persistence number of that integer. Thus, the persistence number of 715 is 3, the persistence number of 27 is 2, the persistence number of 4000 is 1, and the persistence number of 9 is 0.

You are to write a program that will continually prompt the user to enter a positive integer until EOF has been entered via the keyboard. For each number entered your program should output the persistence of the number. Please note that the correct spelling of persistence is p-e-r-s-i-s-t-e-n-c-e. The word does not contain the letter “a”.

Program 29: Simulating Call By Reference

Write a program that contains both a main function and a function called swap. Your main program should read in two integer values and then call the function swap. The swap function will swap the original values and then return back to main. Main will then output the values contained in the original variables. The values should be swapped. No global variables are to be used. You must simulate call by reference by passing the address of the variables to the function swap. You must be able to do this.

Program 30: Passing a Two Dimensional Array

Write a program that will read in 9 integer values into a 3 x 3 two dimensional array. The values should be entered via standard input. The main program should then call a function sum that will calculate the sum of the values and return this sum back to the main program. The main program will output the sum. This program shows that you can successfully pass a two dimensional array in C. You should also pass the number of rows and the number of columns.

Prime 31: Brute Force Primes

Write a program that will read in positive integers and determine if each integer is a prime number or not. Do so the brute force way by testing for divisors between 1 and the number and if exactly two divisors are found, then the number is prime. Your main program must call a function IsPrime.

In so doing, your program should read in positive integers from a file. The name of the file is given on the command line via argv[1]. You should read in the numbers until EOF is reached as you do not know the precise number of numbers in the file.

The number 1 is not prime. The number 2 is prime. Output your answer to standard output.

Problem 32: Perfect Numbers

Greek Mathematicians took a special interest in numbers that are equal to the sum of their proper divisors. A proper divisor of n is any number that evenly divides n that is less than n itself. For example, 6 is a perfect number because it is the sum of 1, 2, and 3 which are the factors of 6 that are less than 6. In other words, when you add up the factors of a number, excluding the number itself, you get the number.

Similarly, the number 28 is a perfect number because 1 + 2 + 4 + 7 + 14 add up to 28.

You are to write a program that contains a function called IsPerfect that takes an integer n and returns true (1) if n is perfect and false (0) otherwise. Your program should output all the perfect numbers between 1 and 100,000.

Problem 33: Area of a Triangle

It is well known that the area of a triangle can be calculated from the length of its three sides. Assume that x, y, and z represents the lengths of the three sides of a triangle. The first step in this procedure is to find the semi-perimeter, as follows:

s = ( x + y + z ) / 2

The area can then be computed by taking the square root of the following quantity

s ( s x) ( s y ) ( s z )

Please write a complete C program that prompts the user to enter the lengths of the sides and then outputs the area of triangle. You may assume that the lengths are floating point values and they do represent a valid triangle. You must use a function call and a function definition. Functions that compute a result should not have a printf statement in them.

Problem 34: Blank Removal

A text file contains a bunch of characters. There are no tab characters within the file.

Write a program that replaces two or more consecutive blanks by a single blank. For example, if the input is

Let’s go to the movies.

then the output would be Let’s go to the movies.

The input from this program should come from a file whose name has been supplied via argv[1]. The output from this program should go to standard output. Note: This program came from Chapter 3 in your ANSI C text book.

Program 35: Left Shift Plus

Create a program that has the following two variable definitions with initializers.

int x = 1 ;

unsigned int y = 1 ;

Then, use the sizeof operator to capture the number of bytes in an int on the machine. Then multiply the number of bytes found by 8 since there are 8 bits/byte. Assign this

value to an identifier named limit. Then place the following inside a for loop ranging from 0 to limit-1.

x = x << 1;

y = y << 1;

printf(“%d %d\n”, x, y ) ;

Before you enter the for-loop print out the initial values of x and y. Make your output look readable. That is, make two nice columns with a label on each column. Are you able to explain the output? Make sure that you can explain the output.

Program 36: Greatest Common Divisor (Brute Force Method)

A file contains an unknown number of pairs of positive integer values. One pair per line. You are guaranteed that each member of the pair of numbers will be a positive integer. Your program will learn the name of the file from argv[1]. You are to write a program that will find the greatest common divisor for each pair of numbers. For this program, you should use a brute force method that will use a loop that will start at the minimum of the two numbers and count down by 1 until the first common divisor of both numbers is found.

Program 37: Greatest Common Divisor (Non-recursive Euclid Method)

This is the same problem as program 36. You should, however, use a non-recursive solution that embodies Euclid’s method.

Program 38: Greatest Common Divisor (Recursive Euclid Method)

A file contains an unknown number of pairs of positive integer values. One pair per line. You are guaranteed that each member of the pair of numbers will be a positive integer. Your program will learn the name of the file from argv[1]. You are to write a program that will find the greatest common divisor for each pair of numbers. You should use a recursive solution that embodies Euclid’s method.You can find the definition of this solution in your textbook on page 228. It is problem 4.17.

Program 39: Non recursive Factorial Use a function!

One way to express the definition of the factorial of a number is

N! is N x (N-1) x (N-2) x (N-3) x (N-4) x … x 3 x 2 x 1

Mathematicians defne 0! to be 1.

For example, 3! is 3 x 2 x 1 which is equal to 6.

Write a C program that computes and outputs to standard output the factorial of a number. Your function main should read in either the value 0 or a positive integer value from the command line and then output the factorial of that number. Your program must call a factorial function. This function cannot and should not print the answer. It should return the number back to the main function. The main function should print the answer. The factorial function should use in iterative solution, non recursive strategy. It should use a

for-loop.

Program 40: Recursive Factorial

Write a complete C program that computes the factorial of a number. This problem is similar to program 39 except that the factorial function must be recursive.

Program 41: Random Numbers 1

Write a C program that will generate 10 random numbers between 0 and 19 inclusive. Your program should call the function rand(). Your main function should call a your own function, say myrand, which then calls rand. Your function would return a single random number. Your program must not use an array.

Program 42: Random Numbers 2

Write a C program that will generate 10 random numbers between -19 and 19. That is a total of 39 possible numbers since 0 should be able to be generated. Define your own function, say myrand, which then calls rand.

Program 43: Malloc Up Space for One Integer

Write a C program that will malloc up space dynamically. The space should hold exactly one integer value. After the space has been reserve, you should deposit the number 6 into the space. After the number 6 has been deposited into the space you should print the value that the space currently holds(that is, 6). You need to create a variable that contains a pointer to an int.

Program 44: Malloc up Space for a 1 Dimensional Array of n Integers

Write a program that will read in a positive integer number, say n, from standard input. Then you need to create enough space on the heap via a call to malloc to hold an array of

n integers. You then need to use the array index operators to fill the array with n integer values from entered by the user via calls to scanf. The index operator is also referred to as a subscript operator (ie., A[i] ). After the array has been filled with numbers, your program should walk down the array and build the sum. Print out the sum.

Program 45: Twin Primes

Report this document

For any questions or suggestions please email
cust-service@docsford.com