Lab Assignments
-
- Fibonacci Series for the first N terms where N is input to the program.
- Finding Maximum of N numbers, where N is input to the program first and then N values are given as input. The values can be both positive or negative integers.
- Finding Maximum of Positive numbers where input acceptance is stopped on user entering -1.
-
- Finding mean of N numbers where N is taken as input and then N values are accepted as input.
- Given N as input and N values as input, read the values into an array. Then, accept a value as input from the user. Write a function which takes this array, N and value as inputs and returns 1 if the value is found in the array and 0 if it is not found. The program must print a meaningful sentence that the value is found or not found in the given set of numbers.
- Finding mode of sorted numbers. Read numbers one by one where the numbers are given in sorted order. At the end, print the mode. The last value encountered with the highest frequency is the mode if more than one number has the same frequency.
-
- String Operations using arrays: Write functions for string copy, string conctenation, string length and string compare. For string compare, return 0 if the strings are equal and 1 otherwise. For string concatenation, write two functions: first one takes only two arguments concat(s1, s2) where s1 is both an input and output parameter and another function with three parameters where the last parameter is the output parameter.
- Write a program that accepts a string as input and determines if the string is a palindrome or not.
- Set operations using arrays: Union, intersection, membership and difference need to be implemented. Subset is an extra requirement that people interested can implement. For union, intersection, difference and subset, the input is the names of two files. Each file contains the integers that are part of the set, one number per line. You have to read from the file into an array. Make this a function as part of modularity. This function can then be reused for many other lab assignments. For membership, accept the name of a file containing the set elements as input. In addition, accept a value and verify if it is a member of the set or not. Also write a function to print the arrays so that the final set can be printed for union, intersection, difference. The return value from the functions in membership and subset is a boolean that returns TRUE (1) if true and FALSE (0) if false. For union, intersection and difference, the parameters to the functions are the two input arrays and a third array that is the output array. The return value from these functions is the number of elements in the output array.
-
- Given two sorted integer arrays, merge them into another sorted array that contains the merged list of integers. For this problem, the integers can be both positive and negative. If a number is duplicated in the input arrays, it MUST occur only once in the output array. In other words, the duplicates are deleted when merging the arrays. Once again, as in sets, the arrays are read from files. So, the names of the files containing the data is the input to the program. Write the merge as a function and call it from the main function. The parameters to this function are the two arrays to be merged and the output array. The return value from the function is the number of elements in the merged array. Use the print array function developed for sets to print the final merged array.
- For this problem, you need to read unsorted numbers from a file one by one and finally print the mode of the data in the file. The values will be in the closed interval [1..R] where R is input to the program. As in sorted mode in lab 2, the last number encountered with the highest frequency is the mode. As you read the values from the file, you need to increment the frequency of the number. In other words, you compute the histogram of the values. Compute the mode using this technique.
-
- Implement GCD as a recursive function. The program accepts two numbers as input and prints the GCD of the two numbers. Make sure that all error conditions are taken care of.
- Exercise 2-5 of Kernighan and Ritchie. This is basically implementing the strpbrk function of the standard C library. This needs to be done using pointers.
-
- Delete from an ordered array all elements that occur more than k times. Accept the name of the file which contains the elements as a command line argument. Use the read array function from sets to read elements into the array. Then, implement the problem and finally output the modified array. Please note that the array is both the input and output parameter to the function and no extra array can be used.
- Accept the name of a file containing text as a command line argument. Write the function that reads the contents of this file and prints the total number of characters, words and lines in the text. In addition, it should also print the longest word in the file with the length of this word. This is similar to the wc command in Linux. Assume that the file contains only letters and digits. The file can contain more than one space separating words or tabs. Please remember that spaces and tabs are part of the total number of characters in the file.
-
- Implement set operations using bits. Assume that a set is an unsigned int. Since typically, an int is 4B in length, this means that there are 32 bits in int. Therefore, the input files for the sets will not contain more than 32 elements. Read the value from the file and set the bit corresponding to the value-1 in the unsigned int. Write a function to do this so that it can be reused for all operations. You need to write bit operation functions: set a bit, get a bit from the given location and also a function which returns TRUE (1) if a bit in a particular location is set and FALSE (0) otherwise. These functions will be used in doing union, intersection, membership and difference.
- Study the itoa() function in Kernighan and Ritchie and implement the exercise 3-5, i.e., itob(). Accept the integer and base as command line arguments.
- Implement the exercise 2-3 in Kernighan and Ritchie, i.e., htoi() function. Accept the hexadecimal string as command line argument.
-
- This is the third mode problem. In this, the values are unsorted and are in a file. The values can be any arbitrary value but there will be at most R unique values. So, you will need to maintain an array of structures, where the structure consists of the value and its frequency. As values are read from the file, you will need to search the array if this value is already present in the array and if so, increment the frequency. Otherwise, add the element to the array. At the end, as usual, the last occurring value is the mode if more than one value has the same highest frequency. Accept the name of the file containing the values and R as command line arguments.
- Implement any sorting function. Accept the name of a file containing values as and the output file name as command line arguments. Read values from the input file into an array, sort and write the sorted array into the output file.
- In this sorting of data is implemented using the qsort() function. The specification is the same as in the previous problem except that instead of using your own sorting function, you will use the qsort() function provided by default in Linux. This requires you to understand and use function pointers. Do man qsort to understand how to use this function.