0% found this document useful (0 votes)
12 views

Key CPDS internal 1

Uploaded by

ANISHYA P IT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Key CPDS internal 1

Uploaded by

ANISHYA P IT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EC22303 C Programming and Data Structures

INTERNAL EXAM 1 KEY


PART-A
1.What are the different data types available in “C”?
 Integer (int)
 Character (char)
 Floating Point (float, double)
 Void (void)
 Boolean (_Bool)
 Enum (enum)
 Array
 Structure (struct)
 Union (union)
 Pointer (pointer)
2.Distinguish between character and string.
Here's a clear differentiation between character and string:
Character:
1. Single symbol or glyph.
2. Enclosed in single quotes (e.g., 'a').
3. Occupies 1 byte of memory.
4. Represents a single ASCII value.
5. Example: 'a', '1', '@'.
String:
1. Sequence of characters.
2. Enclosed in double quotes (e.g., "Hello").
3. Occupies multiple bytes of memory.
4. Represents a collection of ASCII values.
5. Example: "Hello World", "abc123".
3.Differentiate break and continue statement with example.
Break Statement:
1. Terminates the loop immediately.
2. Exits the loop's scope.
3. Execution continues with the next statement after the loop.
Continue Statement:
1. Skips the current iteration.
2. Moves to the next iteration.
3. Does not exit the loop.
Example:
// Break Statement
for (int i = 0; i < 5; i++) {
if (i == 3) {
break; // exit loop when i == 3
}
printf("%d ", i);
}
// Output: 0 1 2
// Continue Statement
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue; // skip iteration when i == 3
}
printf("%d ", i);
}
// Output: 0 1 2 4
4.How the members of structure object is accessed?
In C, members of a structure object can be accessed using two operators:
1. Dot (.) operator
2. Arrow (->) operator (for pointers)
5.Write a C program to print numbers from 10 to 1 using for loop statement.
#include <stdio.h>
int main() {
for(int i = 10; i >= 1; i--) {
printf("%d\n", i);
}
return 0;
}```
6.What is a Pointer? How a variable is declared to the pointer?
What is a Pointer?

A pointer is a variable that stores the memory address of another variable. It "points to"
the location in memory where the variable is stored.
Declaring a Pointer Variable
To declare a pointer variable, use the asterisk symbol (*) before the pointer name.
data_type *pointer_name;
- data_type: Type of data the pointer will point to (e.g., int, char, float).
- pointer_name: Name of the pointer variable.
7.What is a file?
A file is a collection of related data or information stored on a computer's storage
device, such as a hard drive, solid-state drive, or flash drive. Files can contain various
types of data, including:
 Text (documents, notes)
 Images (photos, graphics)
 Audio (music, voice recordings)
 Video (movies, clips)
 Executable programs (software, apps)
 Binary data (compiled code, databases)
8.How addresses are assigned to pointers?
Addresses are assigned to pointers through various methods:
1. Address-of Operator (&): Gets the memory address of a variable.
int x = 10;
int *ptr = &x;
1. Dynamic Memory Allocation: Allocates memory at runtime using functions like
malloc(), calloc(), or realloc().
int *ptr = (int*) malloc(sizeof(int));
1. Array Indexing: Arrays store elements contiguously in memory; pointers can point to
array elements.
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to arr[0]
1. Pointer Arithmetic: Incrementing/decrementing pointers moves them through
memory.

int arr[5] = {1, 2, 3, 4, 5};


int *ptr = arr;
ptr++; // ptr now points to arr[1]
1. Function Arguments: Pointers can be passed as function arguments.
void myFunction(int *ptr) {
// ptr is assigned the address of the argument
}
9.Differentiate structure and union.
Structure
1. Collection of variables of different data types.
2. member occupies separate memory space.
3. Members are accessed using dot (.) operator.
struct Employee {
int id;
char name[20];
float salary;
};
Union
1. Collection of variables of different data types.
2. All members share the same memory space.
3. Only one member can be accessed at a time.
union Data {
int i;
float f;
char str[20];
};
Part-B
10.a. Demonstrate the concept of operator precedence and associtivity with example.
 Operator Precedence: Determines the order in which operators are evaluated
in expressions.
Example: *, /, % (higher precedence) vs. +,- (lower precedence)
Expression: 3 + 5 * 2 → 5 * 2 evaluated first → Result: 13
 Associativity: Defines the direction of evaluation when operators have the same
precedence.
 Left-to-right: +,-,*,/,%
Example: 10 - 5 - 2 → (10 - 5) - 2 → Result: 3
 Right-to-left: =, +=, -=
Example: x = y = 5 → y = 5, then x = 5
10b.Write a C program to do matrix addition and multiplication.
#include <stdio.h> // Header file for I/O functions
int main() { // Main function
int a[10][10], b[10][10], sum[10][10], mul[10][10]; // Arrays for matrices
int i, j, k, row, col; // Loop variables, matrix dimensions
// Input matrix dimensions
printf("Enter row, col: ");
scanf("%d%d", &row, &col);
// Input elements of first matrix
printf("Enter elements of matrix A: ");
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
scanf("%d", &a[i][j]);
// Input elements of second matrix
printf("Enter elements of matrix B: ");
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
scanf("%d", &b[i][j]);
// Matrix addition
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
sum[i][j] = a[i][j] + b[i][j];
// Display result of addition
printf("Matrix Addition: \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
// Matrix multiplication
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
mul[i][j] = 0;
for (k = 0; k < col; k++)
mul[i][j] += a[i][k] * b[k][j];
}
// Display result of multiplication
printf("Matrix Multiplication: \n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
printf("%d ", mul[i][j]);
printf("\n");
}
return 0; // Return statement
}
11a. What is structure? Write a C program to store and display the details of 50
employees.
Structure: A user-defined data type in C that groups different types of variables
(e.g., int, char) under a single name.
Keywords:
struct: Defines a structure.
typedef: Optional, creates an alias for the structure.
printf: Displays output.
scanf: Takes input.
C Program (Keywords only):
#include <stdio.h> // Header for I/O functions
struct Employee { // Structure definition
int id; // Employee ID
char name[50]; // Employee Name
float salary; // Employee Salary
};
int main() {
struct Employee emp[50]; // Array of 50 employees
int i;
// Input employee details
for (i = 0; i < 50; i++) {
printf("Enter details for Employee %d\n", i + 1);
printf("ID: ");
scanf("%d", &emp[i].id); // Employee ID input
printf("Name: ");
scanf("%s", emp[i].name); // Employee Name input
printf("Salary: ");
scanf("%f", &emp[i].salary); // Employee Salary input
}
// Display employee details
printf("\nEmployee Details:\n");
for (i = 0; i < 50; i++) {
printf("ID: %d\n", emp[i].id); // Display ID
printf("Name: %s\n", emp[i].name); // Display Name
printf("Salary: %.2f\n", emp[i].salary); // Display Salary
}
return 0; // Return statement
}
11 b. What is an array? Write a C program to find the largest element in an array
using functions.
An array in C is a collection of elements of the same data type, stored in
contiguous memory locations. Arrays allow you to store multiple values in a single
variable and access them using an index.
#include <stdio.h>
int findLargest(int arr[], int n);
int main() {
int arr[100], n, i, largest;
printf("Enter number of elements: ");
scanf("%d", &n);
rintf("Enter the elements: ");
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = findLargest(arr, n);
printf("Largest element is: %d", largest);
return 0;
}
int findLargest(int arr[], int n) {
int max = arr[0], i;
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
return max;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy