[go: up one dir, main page]

0% found this document useful (0 votes)
6 views4 pages

CT&P Lab WriteUp Assignment1& 2

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

CT&P Lab WriteUp Assignment1& 2

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

ASSIGNMENT 1

Aim: WAP to SWAP (interchange) 2 numbers without using third variable.

Objectives:
1. To understand the concept of variable manipulation using arithmetic operations.
2. To perform swapping of values without using additional memory (i.e., without a
temporary variable).
3. To enhance logical thinking in solving problems with space optimization.
Theory:
1. Variables in C
A variable is a name given to a memory location that is used to store data during the
execution of a program. Variables are essential building blocks in programming and must be
declared before use.
data_type variable_name;
Example:
int age;
float marks;
char grade;
2. Data Types in C
Data types specify the type of data a variable can store. C supports several types of data
types:

Data Type Description Memory Size Example Value

int Integer numbers 2 or 4 bytes 10, -25

float Decimal numbers (single precision) 4 bytes 3.14, -0.5

double Decimal numbers (double precision) 8 bytes 3.14159

char Single character 1 byte 'A', 'z'

3. Format Specifiers in C (%d, %f, etc.)


Format specifiers are used in printf() and scanf() to indicate the type of data being printed or
read.
Specifier Data Type Used In Example Output

%d int printf, scanf printf ("%d", 10);

%f float printf, scanf printf ("%f", 3.14);

%lf double scanf scanf ("%lf", &value);

%c char printf, scanf printf ("%c", 'A');

%s string (char array) printf, scanf printf ("%s", name);

%u unsigned int printf, scanf printf ("%u", 15);

Algorithm:

1. Start
2. Enter x, y
3. Print x, y
4. x = x + y
5. y= x - y
6. x =x - y
7. Print x, y
8. End

Flowchart: (draw on left side blank page)


ASSIGNMENT 2
Aim: Write a C program to find the sum and average of values appearing at the positions divisible by
3 in the given sequence of n values.

Objectives:
1. To develop a C program that processes sequences using array indexing.
2. To identify and access elements at positions (indexes) divisible by 3.
3. To calculate the sum and average of selected elements based on positional logic.
4. To enhance understanding of loop control, indexing, and conditional statements in C
programming.
Theory:
An array in C is a collection of elements of the same data type stored in contiguous
memory locations. Arrays are used to store multiple values under a single variable name,
making data handling and manipulation more efficient, especially when dealing with large
amounts of data.
Declaration and Initialization
data_type array_name[array_size];
Example:
int numbers [10]; // Declares an integer array of size 10
Arrays can also be initialized at the time of declaration:
int numbers[5] = {1, 2, 3, 4, 5};
Accessing Array Elements:
Array elements are accessed using indexing.
In C, indexing starts from 0, so the first element is at index 0, the second at index 1, and so
on.
printf("%d", numbers[2]); // Prints the 3rd element (value 3)

The for loop is commonly used to traverse arrays:


for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
Applications of Arrays
• Storing and processing lists of data (e.g., marks of students, temperatures, etc.)
• Implementing data structures like stacks, queues, and matrices
• Used in algorithms that require grouped data (e.g., sorting and searching)
Advantages of Arrays
• Easy access to data using index
• Efficient use of memory when handling fixed-size data
• Simplifies code when dealing with multiple values of the same type
Limitations of Arrays
• Fixed size; cannot dynamically resize during program execution
• All elements must be of the same data type
• Risk of memory wastage if array is underutilized

Algorithm:
1. Start
2. Initialize variables: totalSum = 0, count = 0, average = 0
3. Input the value of n (number of values in the sequence)
4. Initialize an array named sequence of size n
5. Input n values into the sequence array
6. Loop i from 1 to n:
a. If i is divisible by 3:
i. Add sequence[i] to totalSum
ii. Increment count by 1
7. If count > 0:
a. Calculate average as totalSum / count
8. Display the totalSum and average
9. End
Flowchart: (draw on left side blank page)

You might also like