12.17 Tutorial10
intro-to-computer-science-gtiit-tutorial-10-2024.pdf
1. Programs Outputs
1.1
What is the output of this C program?
main(){
int i;
char c[] = "Nice";
for(i=0; i<5; i++)
printf("%c ",c[i]);
}
N i c e
1.2
What is the output of this C program?
void go(int a[], int j){
printf("%d ", a[j]);
}
main(){
int i;
int a[5] = {3,3,7,3,8};
int b[5] = {2,2,4,2,2};
for(i=0; i<5; i++)
go(b,i);
go(a,b[i-2]));
}
2 2 4 2 2 7
2. Functions Definitions
2.1
In a C file, define the following functions:
-
void printArr(const int a[], int n)
- prints an array of size n on a single line and prints a newline character. -
void fillRandom(int a[], int n)
- fills the array with random values from 0 to 99. This function assumes that the calling environment has already initialized the random number generator withsrand()
. -
void reverse(const int a[], int b[], int n)
- copies the contents of array a in reverse order into array b (n is the size of both arrays).
Use the following main()
function to test your functions:
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define S 10
main(){
srand(time(NULL));
int x[S];
int y[S];
fillRandom(x,S);
reverse(x,y,S);
printArr(x,S);
printArr(y,S);
}
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define S 10
void printArr(const int a[], int n) {
for(int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
void fillRandom(int a[], int n) {
for(int i = 0; i < n; i++)
a[i] = rand() % 100;
}
void reverse(const int a[], int b[], int n) {
for(int i = 0; i < n; i++)
b[i] = a[n - i - 1];
}
int main(){
srand(time(NULL));
int x[S];
int y[S];
fillRandom(x,S);
reverse(x,y,S);
printArr(x,S);
printArr(y,S);
}
2.2
In a C program, define the following functions:
-
int isLower(char c)
- returns 1 if c is a lowercase letter, 0 otherwise. -
int isUpper(char c)
- returns 1 if c is an uppercase letter, 0 otherwise. -
char modChar(char c)
- returns the uppercase counterpart if given a lowercase letter, and vice versa. If given any other kind of character, it returns the same character. This function must callisLower()
andisUpper()
. -
void modString(char str[])
- applies the functionmodChar()
to the zero-terminated string str, character by character.
Use the following main()
function to test your program:
main(){
char input[100];
gets(input);
modString(input);
printf("%s\n", input);
}
Example of execution:
$ tcc -w -run strings.c
Hello World!
hELLO wORLD!
#include<stdio.h>
int isLower(char c) {
if(c >= 'a' && c <= 'z')
return 1;
else
return 0;
}
int isUpper(char c) {
if(c >= 'A' && c <= 'Z')
return 1;
else
return 0;
}
char modChar(char c) {
if(isUpper(c)) {
c = c + 'a' - 'A';
return c;
}
else if(isLower(c)) {
c = c - 'a' + 'A';
return c;
}
else
return c;
}
void modString(char str[]) {
for(int i = 0; str[i] != '\0'; i++) {
str[i] = modChar(str[i]);
}
}
int main(){
char input[100];
gets(input);
modString(input);
printf("%s\n", input);
}
2.3
In a C program, define a function: int search(const int a[], int n, int v)
This function performs a linear search of the value v in the array a of size n. It returns:
- The smallest value i such that v \=\= a[i], if such i exists, otherwise
- The symbolic constant value ERROR (defined as -1).
In the same program, define a main()
function that declares and initializes an array, and does two calls to search()
, to show both behaviors of the function.
#include<stdio.h>
int search(const int a[], int n, int v) {
for(int i = 0; i < n; i++) {
if(a[i] == v)
return 1;
}
}
int main() {
int array[64];
int number = 0;
int curr;
scanf("%d", &curr);
array[0] = curr;
for(int i = 0; curr != 250250250; i++) {
scanf("%d", &curr);
array[i + 1] = curr;
number++;
}
printf("What value do you want to search: ");
int need_search;
scanf("%d", &need_search);
if(search(array, number, need_search) == 1)
printf("Yes");
else
printf("Error");
}