Docsity

Assignment 3 with Solutions - Computer Science I | COP 3502, Assignments of Computer Science

Toggle sidebar
bg1
Due in class: Wednesday July 10th
Problem #1
Assumptions:
1. Prompt the user to enter the value of n, assume that the maximum value of n is 20 for
this program. Your program must check to be sure the user has entered a proper
value for n.
2. User can enter both positive and negative values into the array.
Example execution:
Enter the number of elements in your array (1-20): 7
Enter number 1: -45
Enter number 2: -29
Enter number 3: 56
Enter number 4: -45
Enter number 5: -45
Enter number 6: 0
Enter number 7: 56
Number 56 appeared 2 times
Number 0 appeared 1 time
Number –29 appeared 1 time
Number –45 appeared 3 times
Problem #2
Assignment #3 - 1
Write a C program that reads n integers into an array, then prints on a separate line
the value of each distinct element in the array along with the number of times it occurs
in the array. The values must be printed in descending order. Your program must
use different functions for the following tasks:
1. A function to read in the n integers and place them in the array.
2. A function which determines the distinct values and the number of times each
occurs in the array.
3. A function which prints the distinct values and the number of occurrences for
each in descending order.
COP 3502 – Summer 2002 – Assignment #3
Write a C program that will read in a string of n characters (1 character at a time)
then use a recursive function to reverse the string of characters and place them in
to a second array in the reverse order from the original input order, and finally use
another function to determine if the original string was a palindrome (a word or
string that reads the same forward and backward). Your program should prompt
the user for the original string, print out this string as well as the reverse of the
string, and finally should print a message indicating if the string is a palindrome.
You will write programs in C to solve the following problems. Each program is a stand-
alone program, so the disk you submit will contain three separate programs.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Assignment 3 with Solutions - Computer Science I | COP 3502 and more Assignments Computer Science in PDF only on Docsity!

Due in class: Wednesday July 10th

Problem # Assumptions:

  1. Prompt the user to enter the value of n , assume that the maximum value of n is 20 for this program. Your program must check to be sure the user has entered a proper value for n.
  2. User can enter both positive and negative values into the array. Example execution: Enter the number of elements in your array (1-20): 7 Enter number 1: - Enter number 2: - Enter number 3: 56 Enter number 4: - Enter number 5: - Enter number 6: 0 Enter number 7: 56 Number 56 appeared 2 times Number 0 appeared 1 time Number –29 appeared 1 time Number –45 appeared 3 times Problem # Assignment #3 - 1 Write a C program that reads n integers into an array, then prints on a separate line the value of each distinct element in the array along with the number of times it occurs in the array. The values must be printed in descending order. Your program must use different functions for the following tasks:
  3. A function to read in the n integers and place them in the array.
  4. A function which determines the distinct values and the number of times each occurs in the array.
  5. A function which prints the distinct values and the number of occurrences for each in descending order.

COP 3502 – Summer 2002 – Assignment

Write a C program that will read in a string of n characters (1 character at a time) then use a recursive function to reverse the string of characters and place them in to a second array in the reverse order from the original input order, and finally use another function to determine if the original string was a palindrome (a word or string that reads the same forward and backward). Your program should prompt the user for the original string, print out this string as well as the reverse of the string, and finally should print a message indicating if the string is a palindrome. You will write programs in C to solve the following problems. Each program is a stand- alone program, so the disk you submit will contain three separate programs.

Assumptions:

  1. Prompt the user to enter the value of n , assume that the maximum value of n is 20 for this program. Your program must check to be sure the user has entered a proper value for n.
  2. Don’t actually deal with strings, just use an array of characters for both the original and the reversed “string”. Example Execution: Enter the number characters in your string (1-20): 11 You entered the string: hello world Your string reversed is: dlrow olleh Your string is not a palidrome. Enter the number characters in your string (1-20): 5 You entered the string: radar Your string reversed is: radar Your string is a palidrome: Problem # Assumptions:
  3. The definition for each matrix A, B, and C will be a square matrix which means that the number of rows and the number of columns is the same in each matrix. However, the user may elect to fill only a portion of each matrix. For example the user might set matrix A[4, 5] and matrix B[3, 4]. C will be used as a result matrix so its usable dimensions will be determined by the operation which produces the result in C. For this program use the following to define the matrices: #define maxrows 8 Write a C program that will perform the following operations on three matrices A, B, and C where matrices A and B will be randomly filled with integer values between 1 and 20. Matrix C will be used as a result matrix.
  4. Randomly fill both A and B and print both after filling.
  5. Adds two compatible matrices and prints the sum matrix.
  6. Multiplies two compatible matrices and prints the product matrix.
  7. Transposes and prints the transpose of each matrix A, B, and C.

for (i = 0; i < rows-1; i++) for (j = i+1; j < columns; j++){ temp = A[i][j]; A[i][j] = A[j][i]; A[j][i] = temp; } Example Execution: Enter the row dimension of matrix A: 3 Enter the column dimension of matrix A: 3 Enter the row dimension of matrix B: 3 Enter the column dimension of matrix B: 3 Matrix A is: 2 4 5 3 1 4 2 6 8 Matrix B is: 3 4 1 2 5 1 2 6 2 Sum of A + B is: 5 8 6 5 6 5 4 12 10 Product of A  B is: 24 58 16 19 41 12 34 86 24 Transpose of Matrix A is: 2 3 2 4 1 6 5 4 8 Transpose of Matrix B is: 3 2 2 4 5 6 1 1 2 Transpose of Matrix C is: 24 19 34 58 41 86 16 12 24 Matrix Operations Explained

Matrix Addition for (i = 0; i < n; i++) for (j = 0; j < n; j++) A[i][j] = B[i][j] + C[i][j]; In general, matrix addition is defined only for two matrices of the same size (although they do not need to be square). Thus A[n,m] = B[n,m] + C[n,m] with addition defined as: ^  ^           (b[n, 1 ] c[n, 1 ]) b[n, 2 ] c[n, 2 ]) ...(b[n, m (b[ 2 , 1 ] c[ 2 , 1 ]) b[ 2 , 2 ] c[ 2 , 2 ]) ...(b[ 2 , m (b[ 1 , 1 ] c[ 1 , 1 ]) b[ 1 , 2 ] c[ 1 , 2 ]) ...(b[ 1 ,m ] A [n,m ]  Example: Martix B 2 3 2 1 4 6 3 2 2 Matrix C 4 3 2 1 3 3 2 2 4 Matrix C = A[n,m] + B[n,m] 2 + 4 = 6 3 + 3 = 6 2 + 2 = 4 1 + 1 = 2 4 + 3 = 7 6 + 3 = 9 3 + 2 = 5 2 + 2 = 4 2 + 4 = 6 Matrix Multiplication for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = A[i][j] = 0; k < n; k++) A[i][j] += B[i][k] * C[k][j];

Matrix Transposition for (i = 0; i < n-1; i++) for (j = i+1; j < n; j++){ temp = A[i][j]; A[i][j] = A[j][i]; A[j][i] = temp; } In general, the transpose of a matrix is the matrix that results from the interchanging of its rows and columns. Given a matrix B, its transpose is generally denoted as BT. Example: Matrix B 2 3 2 1 4 6 3 2 2 Matrix BT 2 1 3 3 4 2 2 6 2 Matrix A = B  C (24 + 31 + 22) (23 + 33 + 22) (22 + 33 + 24) (14 + 41 + 62) (13 + 43 + 62) (12 + 43 + 64) (34 + 21 + 22) (33 + 23 + 22) (32 + 23 + 2*4) Thus Matrix A is: 15 19 21 20 27 38 18 19 20

Matrix Transposition for (i = 0; i < n-1; i++) for (j = i+1; j < n; j++){ temp = A[i][j]; A[i][j] = A[j][i]; A[j][i] = temp; } In general, the transpose of a matrix is the matrix that results from the interchanging of its rows and columns. Given a matrix B, its transpose is generally denoted as BT. Example: Matrix B 2 3 2 1 4 6 3 2 2 Matrix BT 2 1 3 3 4 2 2 6 2