Week 10 Worksheet
Note: Try to solve these questions on your own.
Question 1: Find the Errors
The following program has logic and/or compiler errors. Identify 5 errors and for each error:
- Mention the line number where the error occurred.
- Rewrite the line containing the error after fixing it.
Your answer should be in the following format:
- Line
xxshould be:corrected version of line xx - Line
yyshould be:corrected version of line yy
Part 1
#include <stdio.h>
int main() {
int numbers[5];
int sum = 0;
float average;
printf("Enter 5 numbers: ");
// Loop to read input
for (int i = 0; i <= 5; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}
// Calculate average
average = sum / 5;
// Print the result
printf("The average is %.2f\n", average)
// Check for perfect score
if (average = 100) {
printf("Perfect Score!\n");
}
// End message
printf(Program Ended);
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int numbers[5];
int sum = 0;
float average;
cout << "Enter 5 numbers: ";
// Loop to read input
for (int i = 0; i <= 5; i++) {
cin >> numbers[i];
sum += numbers[i];
}
// Calculate average
average = sum / 5;
// Print the result
cout << "The average is " << average << "\n"
// Check for perfect score
if (average = 100) {
cout << "Perfect Score!\n";
}
// End message
cout << Program Ended;
return 0;
}
Part 2
#include <stdio.h>
int sum = 0;
// stores x + y in result and adds the result to the global variable sum.
add(int x, int y, int* result) {
result = x + y;
int sum += (x + y);
}
// returns the absolute value of x
int absolute_value(int x) {
if (x < 0)
return x * -1;
}
int main() {
int x = 7;
int y = -8;
// change y from -8 to 8 using the function absolute_value()
absolute_value(y);
// store in result the value of x + y
int result;
result = add(y, x);
// should print: 7 + 8 = 15.
printf("%d + %d = %d\n", x, y, result);
}
#include <iostream>
using namespace std;
int sum = 0;
// stores x + y in result and adds the result to the global variable sum.
add(int x, int y, int* result) {
result = x + y;
int sum += (x + y);
}
// returns the absolute value of x
int absolute_value(int x) {
if (x < 0)
return x * -1;
}
int main() {
int x = 7;
int y = -8;
// change y from -8 to 8 using the function absolute_value()
absolute_value(y);
// store in result the value of x + y
int result;
result = add(y, x);
// should print: 7 + 8 = 15.
cout << x << " + " << y << " = " << result << "\n";
}
Question 2: What is the Output?
Determine the output of the following code snippet:
int x;
int y;
int *p = &x;
int *q = &y;
*p = 35;
*q = 98;
*p = *q;
printf("%d %d %d %d", x, y, *p, *q);
int x;
int y;
int *p = &x;
int *q = &y;
*p = 35;
*q = 98;
*p = *q;
cout << x << " " << y << " " << *p << " " << *q;
Question 3: Valid or Invalid?
Which of the following statements is NOT valid?
Part 1
Given the declaration:
int x = 5, y = 3;
int p[5] = {5, 6, 7, 8, 9};
int *q = &y;
p = q;*p = 56;*p = *q;q = &x;
Part 2
Given the declaration:
int x = 5, y = 3;
int q[5] = {5, 6, 7, 8, 9};
int *p = &y;
p = q;*p = 56;*p = *q;q = &x;
Question 4: Reverse Array using Pointers
Write a program that asks the user for the number of elements N, reads N integers into an array using pointers, and then prints the array elements in reverse order using pointers.
Example:
Enter number of elements: 5
Enter elements:
1 2 3 4 5
Reversed Array:
5 4 3 2 1
Question 5: Palindrome Check
Write a program that reads a string from the user, calculates its length N, and checks if it is a Palindrome (a string that reads the same from left to right and right to left).
Example:
Enter a string: racecar
The string is a Palindrome.
Question 6: What is the Output?
Determine the output of the following code snippet, which tests your understanding of function parameters (call by value and call by reference).
#include <stdio.h>
int f1(int x, int y) {
x = x + 2;
y = y + 3;
return x + y;
}
int f2(int *x, int y) {
*x = *x + 2;
y = y + 3;
return *x + y;
}
int f3(int *x, int *y) {
*x = *x + 2;
*y = *y + 3;
return *x + *y;
}
int f4(int x, int *y, int *z) {
x = x + *y;
*y = *z + 3;
z = &x;
*z = (*y) * 2;
return *z;
}
int main() {
int k = 3, m = 5, r = 0;
printf("1) %d %d %d \n", k, m, r);
r = f1(k, m);
printf("2) %d %d %d \n", k, m, r);
r = f2(&k, m);
printf("3) %d %d %d \n", k, m, r);
r = f3(&k, &m);
printf("4) %d %d %d \n", k, m, r);
r = f4(k, &m, &r);
printf("5) %d %d %d \n", k, m, r);
return 0;
}
#include <iostream>
using namespace std;
int f1(int x, int y) {
x = x + 2;
y = y + 3;
return x + y;
}
int f2(int *x, int y) {
*x = *x + 2;
y = y + 3;
return *x + y;
}
int f3(int *x, int *y) {
*x = *x + 2;
*y = *y + 3;
return *x + *y;
}
int f4(int x, int *y, int *z) {
x = x + *y;
*y = *z + 3;
z = &x;
*z = (*y) * 2;
return *z;
}
int main() {
int k = 3, m = 5, r = 0;
cout << "1) " << k << " " << m << " " << r << " \n";
r = f1(k, m);
cout << "2) " << k << " " << m << " " << r << " \n";
r = f2(&k, m);
cout << "3) " << k << " " << m << " " << r << " \n";
r = f3(&k, &m);
cout << "4) " << k << " " << m << " " << r << " \n";
r = f4(k, &m, &r);
cout << "5) " << k << " " << m << " " << r << " \n";
return 0;
}
Question 7: Remove Commas
Write a CC++ function called RemoveCommas. The function receives two Strings in the form of character pointers as the following: void RemoveCommas(char * oldval, char * newval).
Your function should generate a new string called newval out of oldval by removing all the commas in oldval.
Example: Given the following code:
char FilteredName[20];
RemoveCommas("v1,v2,v3", FilteredName);
It should store the value "v1v2v3" inside FilteredName.
Constraints:
- You are not allowed to use arrays inside your function, only character pointers.
Question 8: Find Min and Max of Array using Pointers
Write a complete CC++ program including a function FindMinMax() that receives an array pointer, array size, and two pointer variables to store the minimum and maximum values found in the array.
Constraints:
- Do not use array bracket notation
[]inside theFindMinMaxfunction; perform array traversal using only pointer arithmetic and dereferencing.
Example:
Enter number of elements: 5
Enter 5 elements: 12 5 20 8 15
Min: 5
Max: 20
Question 9: Sum Array Range via Pointers
Write a complete CC++ program including a function SumRange() that receives two integer pointers representing the start (inclusive) and end (exclusive) boundaries of an array range, and returns the sum of the elements within that range.
Example:
Enter number of elements: 5
Enter 5 elements: 10 20 30 40 50
Enter start and end indexes: 1 4
Sum: 90