CS 100 Exam Two - Coding - Fall 2016 You are not allowed to use the Internet whi
ID: 3724439 • Letter: C
Question
CS 100 Exam Two - Coding - Fall 2016 You are not allowed to use the Internet while coding the two problems below. You can log into the cs-intro server to test your programs if you wish. When you have finished coding your problems, submit your exam via Blackboard Create a directory called exam2 using mkdir exam2 and move into that directory with cd exam2 Complete the two programs shown below: 1. Name this program case.c-This program repeatedly reads strings from standard input, halting when end- of-file is reached. For each string, it calls a function, countCase, that takes the string and counts the number of upper-case and lower-case characters in the string, returning a count of upper-case and lower-case characters through the last two parameters. Your main routine should then print the number of uppercase and lowercase characters in that string. The function signature for countCase is shown below: void countCase (char *, int *, int *); A sample execution is shown below. User Input: Alabama CRImSON TidE UA Program Output: Alabama: 1 uppercase, 6 lowercase CRImSON: 6 uppercase, 1 lowercase TidE: 2 uppercase, 2 lowercase UA: 2 uppercase, 0 lowercaseExplanation / Answer
For the first program, we will repeatedly enter strings until EOF is encountered using "scanf" in a while loop that will keep inputting strings until an End Of File (-1) is encountered. For using the functions isupper() and islower(), include the header file "ctype.h". We will pass the address of the variables in the function count_case() and then operate on these values using the dereferencing operator (*).
C Code -
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 1000
void count_case(char* a,int* b, int* c) {
// Function to count case in each string
int n = strlen(a); // Length of array
int i; // Counter
for(i = 0; i < n; i++) {
// For isupper() and islower(), include "ctype.h"
// If character is uppercase, increment "up"
if(isupper(a[i]))
(*b)++; // Dereference the pointer to get value
if(islower(a[i]))
(*c)++;
}
}
int main()
{
char s[MAX]; // Char array of size = MAX (1000)
printf("Enter strings till EOF: ");
// Repeatedly enter strings until End-Of-File
while(scanf("%s", &s) != EOF) {
// Initialized the counts for upper and lower case to 0
int up = 0, down = 0;
// Pass the address of these integers to count_case()
count_case(s, &up, &down);
/* After executing this function, "up" and "down" will
contain the counts for upper and lower case letters */
printf("%s: ", s);
printf("%d uppercase, %d lowercase ", up, down);
}
return 0;
}
For the next program, we will read a file containing sorted numbers, store it in an integer array and then print only the unique integers. First, we will open the file and copy its contents into an integer array. I have declared an array of size 1000, you can change the size accordingly. We will initialize this array with a value such as INT_MIN or INT_MAX so that we can demarcate the values read from the file into the array and the remaining array. Use a counter to store the length of the values read from the file.
C Code -
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void init(int* arr, int n) {
// Initialize the array with INT_MIN
int i;
for(i = 0; i < n; i++)
arr[i] = INT_MIN;
// INT_MIN is included in "limits.h"
}
int main()
{
FILE *myfile;
myfile = fopen("filename.txt", "r"); // Open the file in Read mode
int num[1000]; // Array of size 1000
init(num, 1000); // Initialize this array with INT_MIN
int len = 0; // Counter to maintain number of values read from input file
if(myfile == NULL) {
printf("File could not be opened. ");
return 0;
}
int j;
for(j = 0; j < 1000; j++) {
fscanf(myfile, "%d", &num[j]); // Read the value from file, store it in the array
if(num[j] != INT_MIN)
len++; // Increment length if the value is legitimately copied from the input text file
}
printf("Unique numbers are: ");
for(j = 0; j < len; j++) { // Loop over all elements read from file
while(j < len - 1 && num[j] == num[j + 1]) // Keep moving j ahead in case of duplicates
j++;
printf("%d ", num[j]); // Print the last occurence of the element
}
return 0;
}
Note:
To read the file from command line, just change --
int main() to int main(int argc, char **argv) and fopen("filename.txt","r") to fopen(argv[1], "r"). The rest of the program will be the same.