#include <stdio.h>
void update(int *ptr) {
(*ptr) += 50;
}
int main() {
int num = 100;
update(&num);
printf("%d", num);
return 0;
}
Correct Answer: 150
Explanation: The ‘update’ function adds 50 to the value of ‘num’ using a pointer.
#include <stdio.h>
void increment(int *ptr) {
*ptr = *ptr + 1;
}
int main() {
int num = 5;
increment(&num);
printf("%d", num);
return 0;
}
Correct Answer: 6
Explanation: The ‘increment’ function increases the value of ‘num’ by 1 using a pointer.
#include <stdio.h>
void changeValue(int *ptr) {
(*ptr) -= 10;
}
int main() {
int num = 50;
changeValue(&num);
printf("%d", num);
return 0;
}
Correct Answer: 40
Explanation: The ‘changeValue’ function subtracts 10 from the value of ‘num’ using a pointer.
#include <stdio.h>
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] += 10;
}
}
int main() {
int array[] = {1, 2, 3, 4, 5};
modifyArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 11 12 13 14 15
Explanation: The 'modifyArray' function adds 10 to each element of the array.
#include <stdio.h>
void updateArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int array[] = {1, 2, 3, 4, 5};
updateArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 2 4 6 8 16
Explanation: The 'updateArray' function multiplies each element of the array by 2.
#include <stdio.h>
void changeArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] -= i;
}
}
int main() {
int array[] = {5, 7, 9, 11, 13};
changeArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 5 6 8 10 12
Explanation: The 'changeArray' function subtracts the index from each element of the array.
#include <stdio.h>
void modifyValues(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] += i * i;
}
}
int main() {
int array[] = {1, 2, 3, 4, 5};
modifyValues(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 1 3 6 10 15
Explanation: The 'modifyValues' function adds the square of the index to each element of the array.
#include <stdio.h>
void alterArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] *= (i + 1);
}
}
int main() {
int array[] = {2, 3, 4, 5, 6};
alterArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 2 6 12 20 30
Explanation: The 'alterArray' function multiplies each element of the array by its index plus 1.
#include <stdio.h>
void changeArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] -= *(arr + i);
}
}
int main() {
int array[] = {10, 20, 30, 40, 50};
changeArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 0 0 0 0 0
Explanation: The 'changeArray' function subtracts each element of the array from itself.
#include <stdio.h>
void updateArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
*(arr + i) *= i;
}
}
int main() {
int array[] = {1, 2, 3, 4, 5};
updateArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 0 2 6 12 20
Explanation: The 'updateArray' function multiplies each element of the array by its index.
#include <stdio.h>
void modifyArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
*(arr + i) = *(arr + i) * (*(arr + i));
}
}
int main() {
int array[] = {2, 3, 4, 5, 6};
modifyArray(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 4 9 16 25 36
Explanation: The 'modifyArray' function squares each element of the array.
#include <stdio.h>
void alterValues(int *arr, int size) {
for (int i = 0; i < size; i++) {
*(arr + i) /= (i + 1);
}
}
int main() {
int array[] = {10, 20, 30, 40, 50};
alterValues(array, 5);
for (int i = 0; i < 5; i++) {
printf("%d ", array[i]);
}
return 0;
}
Correct Answer: 10 10 10 10 10
Explanation: The 'alterValues' function divides each element of the array by its index plus 1, which for all indices except 0 results in a division by 0. Thus, the output is 10 10 10 10 10.