Answer To: XXXXXXXXXXlabs/lab5.md at master · ITF22519/labs...
Shivani answered on Oct 04 2021
Solution/lab4/Array.txt
10
12
14
65
32
12
46
53
15
67
23
Solution/lab4/lab4_ex1.c
/*Lab4: Exercise 1
Write a program that (1) gets a character from user input (2) gets a string from user input
and (3) calculates the number of input character in the input string. */
#include
#include
int main()
{
char ch;
char str[100];
int count=0;
printf("Enter a character: ");
ch=getchar();
fflush(stdin);
printf("Enter a string: ");
gets(str);
int i=0;
while(*(str+i) != '\0')
{
if (*(str+i) == ch)
count++;
i++;
}
printf("The number of input character in the string is: %d", count);
return 0;
}
Solution/lab4/lab4_ex2.c
/* Lab-4: Ex2
Use pointers to write a program that gets integers from a user, puts the integers in an array,
and prints out the integers in reverse order. */
#include
#include
int main()
{
int n; // number of elements in the array
int *A; // Array
int itr;
printf("\nEnter the size of array:");
scanf("%d", &n);
fflush(stdin);
A = malloc(n*sizeof(int)); // memory allocation
A = (int*)malloc(n*sizeof(int));
if (A== NULL)
{
printf("Error in Memory Allocation");
exit (0);
}
printf("Input values for the array: ");
for(itr=0; itr {
scanf("%d",(A+itr));
}
printf("\nThe input array is:");
for(itr=0;itr printf(" %d",*(A+itr));
printf("\nArray printed in reverse order: ");
for(itr=n-1;itr>=0;itr--)
printf(" %d",*(A+itr));
free(A);
return 0;
}
Solution/lab4/lab4_ex3.c
/* Lab4: Ex3:
Write a program that (a) reads the first element of Array.txt and assigns it to a variable
n, (b) creates an array A with the size n and dynamically allocates memory for A, (c) reads
the next n elements of Array.txt and assigns these elements with the corresponding elements
in array A - if there are not enough n valid elements in Array.txt, print out error message,
(d) prints array A and (e) finds the maximum element of A */
#include
#include
int main()
{
FILE *fp;
int *A;
int n, i;
fp = fopen("Array.txt", "r");
if(NULL == fp)
{
printf("Unable to open file\n");
exit(-1);
}
// reading the first line of the file
fscanf(fp, "%d", &n);
// Allocating memory to the array
A = malloc(n*sizeof(int));
printf("Number of elements in the array: %d",n);
// reading numbers from the file into the array
for(i=0;i {
if(fscanf(fp, "%d", (A+i)) == EOF)
{
printf("\nERROR: File do not contain enough numbers.");
exit(-1);
}
}
// Printing the array elements
printf("\nArray is: ");
for(i=0;i printf("%d ", *(A+i));
// Finding the maximum element of the array
int max=0;
for(i=0;i {
if(*(A+i) > max)
max = *(A+i);
}
printf("\nMaximum element of the array: %d", max);
fclose(fp); //Close the file pointer
return 0;
}
Solution/lab4/lab4_task1.c
/* Lab-4: Task-1: Calculate the number of a character in a string */
#include
int main()
{
char s[] = "This Is The Operating System Course";
char ch = 'e';
int count = 0; // store the number of e in string s
int itr=0; // iterator to iterate through the string
while(s[itr] != 0)
{
if(s[itr] == ch)
{
count++;
}
itr++;
}
printf("The character %c appears %d times\n", ch, count);
return 0;
}
Solution/lab4/lab4_task2.c
/* Lab-4: Task-2: Arithmetic Operators Using Pointer */
#include
int main()
{
int num1, num2;
int *p1, *p2;
int sum, diff, mul, div;
printf("\nEnter one integer: ");
scanf("%d", &num1);
fflush(stdin);
printf("Enter another integer: ");
scanf("%d", &num2);
fflush(stdin);
/* assigning to pointers*/
p1 = &num1;
p2 = &num2;
printf("\nThe first integer is stored at the address: %p", p1);
printf("\nThe second integer is stored at the address: %p", p2);
sum = *p1 + *p2;
diff = *p1 - *p2;
mul = *p1 * *p2;
div = *p1 / *p2;
printf("\nSummation: %d", sum);
printf("\nDifference: %d", diff);
printf("\nMultiplication: %d", mul);
printf("\nDivision: %d", div);
}
Solution/lab4/lab4_task3.c
/* Lab-4: Task-3: Fixing bugs */
#include
void change(int *ptr1, int *ptr2)
{
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
int main()
{
int num1, num2;
printf("\nEnter the first number: ");
scanf("%d", &num1);
printf("\nEnter the second number: ");
scanf("%d", &num2);
change(&num1, &num2);
printf("\n\nAfter changing two numbers:");
printf("\nThe first number is: %d", num1);
printf("\nThe second number is: %d\n", num2);
return 0;
}
Solution/lab4/lab4_task4.c
/* Lab-4: Task-4: program that prints out the number of characters in a string */
#include
int calStringLength(char *tmpStr);
int main()
{
char str[20];
int length;
printf("Enter a string you want to count: ");
gets(str);
length = calStringLength(str);
printf("Its length is: %d", length);
}
int calStringLength(char *tmpStr)
{
int count = 0;
while (*tmpStr != '\0')
{
count++;
tmpStr++;
}
return(count);
}
Solution/lab4/lab4_task5.c
/* Lab-4: Task-5: **
* code to read the content of file Array.txt which includes 10 integers and put
these integers into an array A. Write your code to (a) print each element of A on the
terminal, (b) calculate the summation of all elements in A, and (c) print the summation on
the terminal. **/
#include
#include
int main()
{
FILE *fp;
int *A;
int n=10, i;
fp = fopen("Array.txt", "r");
if(NULL == fp)
{
printf("Unable to open file\n");
exit(-1);
}
// Allocating memory to the array
A = malloc(10*sizeof(int));
// reading numbers from the file into the array
for(i=0;i fscanf(fp, "%d", (A+i));
// Printing the array elements
printf("\nArray is: ");
for(i=0;i printf("%d ", *(A+i));
// Finding the maximum element of the array
int sum=0;
for(i=0;i sum += *(A+i);
printf("\nSum of elements of the array: %d", sum);
fclose(fp); //Close the file pointer
return 0;
}
Solution/lab4/lab4_task6.c
/* Lab-4: Task-6: Dynamic memory allocation with an array */
#include
#include
int main()
{
int n; // number of elements in the array
int *A; // Array
int itr;
// YOUR code to get array size, put it in variable n
printf("\nNumber of elements in the array:");
scanf("%d", &n);
fflush(stdin);
// End of array size
A = malloc(n*sizeof(int)); // memory allocation
// MY code for memory allocation
A = (int*)malloc(n*sizeof(int));
if (A== NULL)
{
printf("Error in Memory Allocation");
exit (0);
}
// END of my code
// YOUR code to fill out elements of the array
printf("Enter array elements: ");
for(itr=0; itr {
scanf("%d",(A+itr));
}
// YOUR code to print the array
printf("\nArray:");
for(itr=0;itr printf(" %d",*(A+itr));
// Your code for memory deallocation
free(A);
// DONE!
return 0;
}
Solution/lab4/lab4_task7.c
/* Lab4: Task7: Dynamic memory allocation with two dimensional array*/
#include
#include
int main()
{
int m, n;
printf("Enter the nmuber of rows and columns for matrix: ");
scanf("%d%d", &m, &n);
int **a;
//Allocate memory to matrix
a = (int **) malloc(m * sizeof(int *));
for(int i=0; i {
a[i] = (int *) malloc(n * sizeof(int));
}
//YOUR CODE HERE
// Fill-out matrix from a user input
printf("Enter matrix elements:");
for(int i=0;i {
for(int j=0;j scanf("%d", (a+ i*n +j));
}
// Matrix printing.
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%d ", *(a + i*n + j));
printf("\n");
}
// Memory deallocation
free(a);
// END OF YOUR CODE
return 0;
}
Solution/lab5/cal.c
#include
#include
#include
#include
#include
#include
void main (int argc, char *argv[])
{
if(argc < 4)
{
printf("usage: cal \n");
exit(0);
}
int num1 = atoi(argv[2]);
int num2 = atoi(argv[3]);
int result = 0;
if(strcmp(argv[1], "add") == 0)
{
result = num1 + num2;
}
else if(strcmp(argv[1], "sub") == 0)
{
result = num1 - num2;
}
else if(strcmp(argv[1], "mul") == 0)
{
result = num1 * num2;
}
else if(strcmp(argv[1], "div") == 0)
{
result = num1/num2;
}
else
{
printf("Unknown Operator: %s\n", argv[1]);
exit(0);
}
printf("%d %s %d equals %d\n", num1, argv[1], num2, result);
}
Solution/lab5/exe1.c
#include
#include
int main()
{
execl("/bin/ls", "ls", NULL);
printf("What happened?\n");
}
Solution/lab5/exec_cal.c
/* exec_cal.c : This program reads input from the user for
the operator (add, mul, div, sub), num1 and num2. Then it creates a new child process, and executes the cal program with the collected
arguments from the user. */
#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
pid_t pid;
char op[10];
int num1, num2;
int return_status;
printf("Enter the operator (add, sub, mul, div)\n");
if(scanf("%s", op) != 1)
{
printf("Cannot read operator");
exit(-1);
}
//TODO: check if operator is correct (add, sub, mul, div).
//Otherwise, print an error message and exit
//Hint: check return value of scanf
if((strcmp(op,"add") != 0) && (strcmp(op,"sub") != 0) && (strcmp(op,"mul") != 0) && (strcmp(op,"div") != 0))
{
printf("Invalid Operator. Try again with a correct operator (add, sub, mul, div\n");
exit(-1);
}
printf("Enter the first number\n");
//TODO: Read an integer from user input and store inside num1.
//On eror, print an error message and exit.
if(scanf("%d", &num1) != 1)
{
printf("Cannot read operator");
exit(-1);
}
printf("Enter the second number\n");
//TODO: Read an integer from user input and store inside num2.
//On eror, print an...