Arrays
unknown
c_cpp
a month ago
3.9 kB
2
Indexable
Never
#include <stdio.h> // Function to reverse an array void reverseArray(int arr[], int size) { int start = 0, end = size - 1; while (start < end) { // Swap the elements int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } // Function to count the number of positive integers in an array int countPositive(int arr[], int size) { int count = 0; for (int i = 0; i < size; i++) { if (arr[i] > 0) { count++; } } return count; } int main() { // Problem 1: Create an array of 10 numbers and verify (ptr+2) points to the third element int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int *ptr = arr; printf("Problem 1: Pointer arithmetic verification:\n"); printf("First element: %d\n", *ptr); printf("Third element using (ptr+2): %d\n\n", *(ptr + 2)); // This will print the 3rd element // Problem 2: If S[3] is a 1-D array of integers, *(S+3) refers to the third element printf("Problem 2: *(S+3) refers to the third element? Answer: True\n\n"); // Problem 3: Create an array of 10 integers and store a multiplication table of 5 int table5[10]; printf("Problem 3: Multiplication table of 5:\n"); for (int i = 0; i < 10; i++) { table5[i] = 5 * (i + 1); printf("%d ", table5[i]); } printf("\n\n"); // Problem 4: Repeat Problem 3 for a general input provided by the user using scanf int n; printf("Problem 4: Enter a number to generate its multiplication table: "); scanf("%d", &n); int tableN[10]; for (int i = 0; i < 10; i++) { tableN[i] = n * (i + 1); printf("%d ", tableN[i]); } printf("\n\n"); // Problem 5: Reverse an array printf("Problem 5: Reversing the array from Problem 3:\n"); reverseArray(table5, 10); for (int i = 0; i < 10; i++) { printf("%d ", table5[i]); } printf("\n\n"); // Problem 6: Count the number of positive integers in an array int mixedArr[10] = {-1, 4, -3, 7, 8, -5, 6, -9, 2, -6}; printf("Problem 6: Counting the positive integers in mixedArr:\n"); int positiveCount = countPositive(mixedArr, 10); printf("Number of positive integers: %d\n\n", positiveCount); // Problem 7: Create a 3x10 array containing multiplication tables of 2, 7, and 9 int tables[3][10]; printf("Problem 7: Multiplication tables of 2, 7, and 9:\n"); for (int i = 0; i < 10; i++) { tables[0][i] = 2 * (i + 1); tables[1][i] = 7 * (i + 1); tables[2][i] = 9 * (i + 1); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) { printf("%d ", tables[i][j]); } printf("\n"); } printf("\n"); // Problem 8: Repeat Problem 7 for a custom input given by the user int m1, m2, m3; printf("Problem 8: Enter three numbers for custom multiplication tables: "); scanf("%d %d %d", &m1, &m2, &m3); int customTables[3][10]; for (int i = 0; i < 10; i++) { customTables[0][i] = m1 * (i + 1); customTables[1][i] = m2 * (i + 1); customTables[2][i] = m3 * (i + 1); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) { printf("%d ", customTables[i][j]); } printf("\n"); } printf("\n"); // Problem 9: Create a three-dimensional array and print the address of its elements in increasing order int arr3D[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; printf("Problem 9: Addresses of 3D array elements in increasing order:\n"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { printf("Address of arr3D[%d][%d][%d]: %p\n", i, j, k, &arr3D[i][j][k]); } } } return 0; }
Leave a Comment