PIC practical Flashcards

1
Q

multidimensional array
input and printing of matrix

A

include <stdio.h></stdio.h>

int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf(“%d “, matrix[i][j]);
}
printf(“\n”);
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

add 2 matrices of size n by n

A

include <stdio.h></stdio.h>

#define MAX_SIZE 10
int main() {
int n;
printf(“Enter the size of the matrix:”);
scanf(“%d”,&n);
int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE], sum[MAX_SIZE][MAX_SIZE], matrix[MAX_SIZE][MAX_SIZE];
printf(“enter elements of matrix1:\n “);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf(“%d”,&matrix1[i][j]);
}
}
printf(“enter elements of matrix2:\n “);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf(“%d”,&matrix2[i][j]);
}
}
printf(“sum:\n “);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum[i][j]=matrix1[i][j]+matrix[i][j];
matrix[i][j]=sum[i][j];
printf(“%d\t”,matrix[i][j]);
}
printf(“\n”);
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

read and display a 3 by 3 matrix

A

include <stdio.h></stdio.h>

int main() {
int matrix[3][3];
printf(“Enter elements of the matrix:\n”);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf(“%d”, &matrix[i][j]);
}
}
printf(“The matrix you entered:\n”);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf(“%d “, matrix[i][j]);
}
printf(“\n”);
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

print the sum of diagonal elements of 2-D matrix.

A

include <stdio.h></stdio.h>

int main() {
int matrix[3][3];
int sum = 0;
printf(“Enter elements of the matrix:\n”);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf(“%d”, &matrix[i][j]);
}
}
for (int i = 0; i < 3; i++) {
sum += matrix[i][i];
}
printf(“Sum of diagonal elements: %d\n”, sum);
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

initialize one-dimensional array

A

include <stdio.h></stdio.h>

#define SIZE 5
int main() {
int numbers[SIZE] = {10, 20, 30, 40, 50};
printf(“Array elements: “);
for (int i = 0; i < SIZE; i++) {
printf(“%d “, numbers[i]);
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Consider structure name as Student . Accept the data of one student and display it using pointer to structure

A

include <stdio.h></stdio.h>

struct Student {
char name[100];
int age;
float marks;
};
int main() {
struct Student student, *ptr = &student;
printf(“Enter the name of the student: “);
scanf(“%s”, ptr->name);
printf(“Enter the age of the student: “);
scanf(“%d”, &ptr->age);
printf(“Enter the marks of the student: “);
scanf(“%f”, &ptr->marks);
printf(“\nStudent Information:\n”);
printf(“Name: %s\nAge: %d\nMarks: %.2f\n”, ptr->name, ptr->age, ptr->marks);
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to declare and initialize a Two-dimensional array? Discuss with examples.

A

include <stdio.h></stdio.h>

#define ROWS 3
#define COLS 4
int main() {
int matrix[ROWS][COLS] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf(“%d “, matrix[i][j]);
}
printf(“\n”);
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Array of structure

A

include <stdio.h></stdio.h>

struct Student {
char name[100];
int age;
float marks;
};

int main() {
struct Student students[3];
for (int i = 0; i < 3; i++) {
printf(“Enter details of Student %d:\n”, i + 1);
printf(“Name: “);
scanf(“%s”, students[i].name);
printf(“Age: “);
scanf(“%d”, &students[i].age);
printf(“Marks: “);
scanf(“%f”, &students[i].marks);
printf(“\n”);
}
printf(“Student Details:\n”);
for (int i = 0; i < 3; i++) {
printf(“Student %d:\n”, i + 1);
printf(“Name: %s\nAge: %d\nMarks: %.2f\n\n”, students[i].name, students[i].age, students[i].marks);
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a C Program to declare the structure of the book with data member book, price and pages for 5 books. Accept data and display the data for 5 books. make it very short and simple

A

include <stdio.h></stdio.h>

struct Book {
char name[100];
float price;
int pages;
};

int main() {
struct Book books[5];

for (int i = 0; i < 5; i++) {
    printf("Enter details of Book %d:\n", i + 1);
    printf("Name: ");
    scanf("%s", books[i].name);
    printf("Price: ");
    scanf("%f", &books[i].price);
    printf("Pages: ");
    scanf("%d", &books[i].pages);
    printf("\n");
}

printf("Book Details:\n");
for (int i = 0; i < 5; i++) {
    printf("Book %d:\n", i + 1);
    printf("Name: %s\nPrice: %.2f\nPages: %d\n\n", books[i].name, books[i].price, books[i].pages);
}

return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly