17. Write a program to print the multiplication table up to 10 of a given integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int number, i; // Ask the user to enter a number printf("Enter an integer: "); scanf("%d", &number); // Print the multiplication table up to 10 printf("\nMultiplication Table of %d:\n", number); for(i = 1; i <= 10; i++) { printf("%d x %d = %d\n", number, i, number * i); } return 0; } |
18. Write a program to find the second greatest element in an integer array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include <stdio.h> #include <limits.h> int main() { int n, i; // Ask for the number of elements printf("Enter the number of elements in the array: "); scanf("%d", &n); if (n < 2) { printf("Need at least two elements to find the second largest.\n"); return 1; } int arr[n]; printf("Enter %d integers:\n", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } int first = INT_MIN, second = INT_MIN; for (i = 0; i < n; i++) { if (arr[i] > first) { second = first; first = arr[i]; } else if (arr[i] > second && arr[i] != first) { second = arr[i]; } } if (second == INT_MIN) { printf("There is no second greatest element (all elements may be equal).\n"); } else { printf("The second greatest element is: %d\n", second); } return 0; } |
19. Write a program to create a structure named ‘ Book’ with data members, bookname, author and price. List the name of those books having price > 500.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <stdio.h> #include <string.h> struct Book { char bookname[50]; char author[50]; float price; }; int main() { int n; printf("Enter number of books: "); scanf("%d", &n); struct Book books[n]; // Input book details for(int i = 0; i < n; i++) { printf("\nBook %d:\n", i + 1); printf("Enter book name: "); scanf(" %[^\n]", books[i].bookname); // Read string with spaces printf("Enter author name: "); scanf(" %[^\n]", books[i].author); printf("Enter price: "); scanf("%f", &books[i].price); } // Display books with price > 500 printf("\nBooks with price greater than 500:\n"); for(int i = 0; i < n; i++) { if(books[i].price > 500) { printf("Book: %s | Author: %s | Price: %.2f\n", books[i].bookname, books[i].author, books[i].price); } } return 0; } |
20.Write a program to add two matrices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <stdio.h> int main() { int rows, cols, i, j; // Input the number of rows and columns printf("Enter number of rows and columns of the matrices: "); scanf("%d %d", &rows, &cols); int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols]; // Input matrix 1 printf("Enter elements of first matrix:\n"); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { scanf("%d", &matrix1[i][j]); } } // Input matrix 2 printf("Enter elements of second matrix:\n"); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { scanf("%d", &matrix2[i][j]); } } // Add the two matrices for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { sum[i][j] = matrix1[i][j] + matrix2[i][j]; } } // Print the result printf("Sum of the two matrices:\n"); for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf("%d ", sum[i][j]); } printf("\n"); } return 0; } |
21. Why do we need storage class? List some graphics functions. How do you pass and return pointer in function? Illustrate with an example.
storage classes define the scope, lifetime, and initial value of a variable, as well as its storage location (memory or CPU register). They determine how and where a variable will be stored and for how long it will exist.
common graphics functions include:
initgraph()
: Initializes the graphics mode and drivers.closegraph()
: Closes the graphics mode.setcolor()
: Sets the current drawing color.setfillstyle()
: Sets the fill pattern and color for shapes.line()
: Draws a line between two specified points.circle()
: Draws a circle with a given center and radius.rectangle()
: Draws a rectangle with specified top-left and bottom-right corners.arc()
: Draws an arc.
Passing a Pointer to a Function (Call by Reference):
When you pass a pointer to a function, you are essentially passing the memory address of a variable. This allows the function to directly access and modify the original variable in the calling function’s scope. This is known as “call by reference.”
- Syntax: The function parameter type will be a pointer (e.g.,
int *ptr
). - Call: You pass the address of the variable using the address-of operator (
&
) (e.g.,&myVariable
).
Returning a Pointer from a Function:
A function can return a pointer. This means the function returns a memory address. You must be careful when returning pointers:
-
Do NOT return pointers to local (automatic) variables: Local variables are allocated on the stack and are destroyed when the function exits. Returning a pointer to such a variable results in a “dangling pointer,” which points to deallocated memory, leading to undefined behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> #include <stdlib.h> // For malloc and free // Function to increment an integer value using a pointer // Pass by reference: 'numPtr' receives the address of 'value' void incrementValue(int *numPtr) { printf("Inside incrementValue: Value before increment = %d\n", *numPtr); (*numPtr)++; // Dereference numPtr to access and modify the actual value printf("Inside incrementValue: Value after increment = %d\n", *numPtr); } // Function to create a new integer and return its address // Returns a pointer to dynamically allocated memory int *createAndReturnValue(int val) { int *ptr = (int *) malloc(sizeof(int)); // Dynamically allocate memory for an integer if (ptr == NULL) { printf("Memory allocation failed!\n"); return NULL; // Return NULL on failure } *ptr = val; // Store the value in the allocated memory return ptr; // Return the address of the allocated memory } int main() { int myValue = 10; int *newValuePtr; printf("Main: Initial myValue = %d\n", myValue); // --- Passing a pointer to a function --- // Pass the address of myValue to incrementValue incrementValue(&myValue); printf("Main: myValue after incrementValue call = %d\n", myValue); // myValue is now 11 printf("\n"); // --- Returning a pointer from a function --- // Call createAndReturnValue, which returns a pointer to new memory newValuePtr = createAndReturnValue(25); if (newValuePtr != NULL) { printf("Main: Value returned by createAndReturnValue = %d\n", *newValuePtr); // Remember to free dynamically allocated memory to prevent memory leaks free(newValuePtr); newValuePtr = NULL; // Good practice: set pointer to NULL after freeing printf("Main: Dynamically allocated memory freed.\n"); } else { printf("Main: Failed to create new value.\n"); } return 0; } |