#include <stdio.h> #define ROWS 3 #define COLS 3 void matrixMultiply(int *mat1, int *mat2, int *result, int rows1, int cols1, int cols2) { int i, j, k; // Multiplying matrices for (i = 0; i < rows1; i++) { for (j = 0; j < cols2; j++) { *(result + i * cols2 + j) = 0; for (k = 0; k < cols1; k++) { *(result + i * cols2 + j) += *(mat1 + i * cols1 + k) * *(mat2 + k * cols2 + j); } } } } void displayMatrix(int *mat, int rows, int cols) { int i, j; // Displaying matrix for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { printf("%d\t", *(mat + i * cols + j)); } printf("\n"); } } int main() { int mat1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int mat2[ROWS][COLS] = {{9, 8, 7}, {6, 5, 4}, {
Question :- Using pointers, write a C program to swap the values of two variables.
#include <stdio.h>
#include<conio.h>
void main()
{
int first_value, second_value, *first_pointer, *second_pointer, temp;
printf("Enter the value of First Value and Second Value\n");
scanf("%d%d", &first_value, &second_value);
printf("Before Swapping\nfirst_value = %d\nsecond_value = %d\n", first_value, second_value);
first_pointer = &first_value;
second_pointer = &second_value;
temp = *second_pointer;
*second_pointer = *first_pointer;
*first_pointer = temp;
printf("After Swapping\nfirst_value = %d\nsecond_value = %d\n", first_value, second_value);
getch();
}
write a C program to swap the values of two variables Using pointers
- int first_value, second_value, *first_pointer, *second_pointer, temp; it's declare variable two variable first_value and second_value it using to input a value by user or defined two pointer variable *first_pointer and *second_pointer it using swaping value using pointer and last variable defined temp is empty value.
- printf("Enter the value of First Value and Second Value\n"); print a message.
- scanf("%d%d", &first_value, &second_value); it is get a value by user %d is a using int data type and & sign using for address of memory block , &first_value is store in memoey , &second_value is store a value in memory block.
- printf("Before Swapping\nfirst_value = %d\n second_value = %d\n", first_value, second_value); print a message to understand the user next step show in this message.
- first_pointer = &first_value; it's assign a address of storing memory block in first pointer.
- second_pointer = &second_value; it's assign address of storing memory block in second pointer.
- temp = *second_pointer; temp is empty variable then assign a value of storing value in second pointer into temp variable.
- *second_pointer = *first_pointer; after assign second pointer into temp then second pointer is empty then assign value in first pointer into second pointer then empty first pointer.
- then first pointer is empty then temp assign a value in first pointer
Comments
Post a Comment