Due date: 10AM one week after the assignment was handed out.
Required Reading: Chapters 2,3,4,5 [IACU]
Try to keep your files organized. For example create a directory for this class and create a subdirectory for assignment 2.
Write the first program in a file called "is_pal.c".
Do the same with second and third exercises.
Write a program "is_pal.c" that identifies a palindrome. A palindrome is a word that is read the same way backwards, for example: adogoda or abccba. Your program should receive the word as parameter, and output a yes/no answer, for example:
% is_pal abigcatacgiba The word abigcatacgiba is a palindrome. % is_pal abigcat The word abigcat is not a palindrome. %
Print error messages to stderr. You can assume that the longest word read has no more than 256 characters. Use fixed characters arrays to store strings.
Hint: In order to go through the characters you should know the length of
the string (there are other ways to implement this using pointers, but you
should use the following:). Use the library function strlen that gives the
number of characters in a string not including the null character. This
function is declared in string.h. Use an array of characters
to store a string.
Don't forget to comment your code !!!
Write a program "p2.c" that reads a word from the standard input. If the
word is in lowercase letters, convert it to uppercase letters ("blabla" becomes
"BLABLA"). If the word is all uppercase letters convert it to lowercase
letters ("SOMETHING" becomes "something"). Otherwise, reverse it (meaning:
"abCd2e" becomes "e2dCba"). Write the result to standard output. You can
assume that the string is not longer than 256 characters.
You can prompt the user to type the word.
Here's a nice exercise: DO NOT use printf anywhere in your program,
use fprintf(stdrr,....) whenever you print a message (even if it's
not an error message), and use fprintf(stdout,...) only to print
the manipulated string. Use scanf as usual. Run your program from DOS
shell (CVN students working on PC, remember to cd to the directory
where the executable of your program resides) and try running it once
using standard output (your screen) and once using an output file
(use %p2 >out.txt).
What happens when you run your program this way: %p2 out.txt
2>msg.txt ?
Hint: In order to go through the characters you should know the length of the string (there are other ways to implement this using pointers, but you should use the following:). Use the library function strlen that gives the number of characters
in a string not including the null character. This function is declared in
string.h. Use an array of characters to store a string.
Don't forget to comment your code !!!
Sorting data (i.e. placing the data into some particular order, such as ascending or descending) is one of the most important computing applications. Virtually every organization must sort some data and in many cases massive amounts of data. In this exerc
ise, you will have to sort a set of integers stored in an array. The algorithm that you will be implementing is called counting sort. It works only for arrays of positive integers. If the n integer elements of the array actually lie in the r
ange 0 to k and k is not too big (compared to n) then this sorting strategy is faster than other known sorting functions, like quicksort and mergesort. (don't worry if you have never heard of all these sorting algorithms
before - all the information you need to implement this exercise is
written here).
You may assume that the number of elements does not exceed 100 and that the biggest number that can occur is 199.
You need two arrays. An input/output array of size n and a temporary array temp of size k (both arrays can have predefined size of 100 and 200 respectively, but you will just use the part up to its real size). You go through the in put array and in the temporary array you keep track of how many occurrences of each number from 0 to 199 are there in the input array. Thus after this first stage temp[i] holds the number of input elements equal to i for each integer i=0,1...k. Now you just have to go through the temporary array from start to end and fill (overwrite) the original array cell by cell with the next item from the temporary array, accounting for the correct number of occurrences for each nonzero item in temp[i].
For an array of eight integers, when the range is 0..6 : input array: 3-6-4-1-3-4-1-4 temporary array: 0-2-0-2-3-0-1 (after first stage) output array: 1-1-3-3-4-4-4-6 (after second stage)
You will have to write a program that will implement the
counting sort algorithm. Your program first reads the numbers of
integers to be read, then reads the list of integers.
Note: The input to your program should be the name of an input
file. In
order to open the file for reading you should prompt the user to give
you the name of that input file, and also a name for an output file
where the sorted array will be written. This is more useful than typing a list of integers every time you run your program to test it!
Note: you should not read the name of the input and output files as
parameters to your program, nor assume fixed filenames. prompt the
user!
Here's an example where the input file ("inp.txt") looks like that:
6 10 8 3 1 5 2
And running the program:
Please enter name of input file: inp.txt Please enter name of output file: out.txt
Now the file "out.txt" should look like this:
1 2 3 5 8 10
Don't forget to check correct opening of stream and print error messages to stderr !!!
Write a file named "README". This file should contain, for each exercise, a list of the files used, a short description of what your program does, and any comments you have and explanations of the way you implemented the assignment, if it's different f rom the required implementation.
Pointers Warm-Up:
Given the declarations and data shown below, evaluate each of the expressions and state its value. Evaluate each expression with the original data shown (that is, the results of one expression do not affect the following ones). Assume that the ints
array begins at location 100 in memory (remember example from class?) and that integers and pointers both occupy four bytes.
int ints[20] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200}; int *ip = ints + 3; (I gave you some of the answers..) Expression Value Expression Value ints 100 ip ints[4] 50 ip[4] ints + 4 ip + 4 *ints + 4 *ip + 4 *(ints + 4) *(ip + 4) ints[-2] #undefined ip[-2] &ints[0] &ip[0] &ints[4] &ip[4]