Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Pop Assign2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

1 Explain the process of function declaration and

definition with an example program along with output.


In the C programming language, a function declaration provides information about a function's
name, return type, and parameters.

A function definition, on the other hand, contains the actual implementation or body of the
function.

// Function declaration

int add(int a, int b);

int main()
{

// Function call

int result = add(3, 5)

// Output the result

printf("Sum: %d\n", result)

return 0;

// Function definition

int add(int a, int b)


{

return a + b;

OUTPUT :
Sum: 8
2. Define an array. How do you declare and initialize
2D array. Explain with an example code.

In C,
an array is a collection of elements of the same data type stored in
contiguous memory locations.
A 2D array is essentially an array of arrays, forming a grid or matrix.
To declare and initialize a 2D array in C, you specify the number of
rows and columns.

#include <stdio.h>
int main()
{
// Declare and initialize a 2D array
int matrix[3][4];
// Access and print elements of the 2D array
printf("Matrix elements:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
3 a) Write a C program to swap two numbers using call by
reference method along with an output.

#include <stdio.h>
void swapNumbers(int a, intb);
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Display original numbers
printf("Original numbers: num1 = %d, num2 = %d\n", num1, num2);
// Call the function to swap numbers
swapNumbers(&num1, &num2);
// Display swapped numbers
printf("Swapped numbers: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
// Function definition to swap numbers using call by reference
void swapNumbers(int a, int b)
{
int temp = a;
a =b;
b = temp;
}
3. b) Write a C program to search an element using binary search with appropriate
messages along with an output.
#include<stdio.h>

int main()

int i, n, a[10], mid, low, high, key, found = 0;

printf("\n Enter the number of elements:\n");

scanf("%d", &n);

printf("Enter the array element in ascending order\n");

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

printf("\n Enter the key element to be searched\n");

scanf("%d", &key);

low = 0;

high = n - 1;

while (low <= high)


{

mid = (low + high) / 2;

if (key == a[mid])

found = 1;

break;

else if (key > a[mid])

low = mid + 1;

else

high = mid - 1;

if (found == 1)

printf("Item found in position : %d\n", mid + 1);

else

printf("\n Item not found\n");

return 0;

}
4.b) Write a C program to sort the elements using bubble sort
by passing array as function argument along with an output.
#include<stdio.h>
int main()
{
int a[100],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the %d elements of array\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The Input array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
If(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
5. a) Write a C program to find nCr along with an output
#include <stdio.h>
int main()
{
int n, r;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
if (n < 0 || r < 0)
printf("n and r must be non-negative integers.\n");
else if (r > n)
printf("r must be less than or equal to n.\n");
else
{
// Calculate nCr using a single function
int nCr = 1;
for (int i = 1; i <= r; i++) {
nCr *= (n - i + 1);
nCr /= i;
}
printf("C(%d, %d) = %d\n", n, r, nCr)
return 0;
}
b) Define a string. List the string manipulation functions. Explain any
two with examples.

A string in C is an array of characters ending with a null character ('\0').


Strings in C are handled as character arrays, and a null character marks
the end of the string. For example:
char myString[] = "Hello, World!";
Here, myString is a character array containing the characters 'H', 'e',
'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'.

STRING MANIPULATION FUNCTIONS IN C:


1) strlen – STRING LENGTH
2) strcpy – STRING COPY
3)
4)
5)
6)
7)
6 a) Write a C program to find the length of a string along with an output.

#include <stdio.h>
#include <string.h>
int main()
{
char myString[] = "Hello, World!";
int length = strlen(myString);
printf("Length of the string: %zu\n", length);
return 0;
}

Length of the string: 13


b) What is pointer? How do you declare and initialize? Explain
with an example code.

Pointer in C:

A pointer in C is a variable that stores the memory address of another variable. It "points to" the
memory location where the value of another variable is stored. Pointers are powerful and widely
used in C for tasks such as dynamic memory allocation, array manipulation, and accessing
functions through pointers.

Declaring and Initializing Pointers:

To declare a pointer in C, you use the asterisk (*) symbol followed by the data type of the variable
it will point to. For example, int* declares a pointer to an integer. You can also initialize a pointer
with the memory address of a variable.

#include <stdio.h>
int main()
{
int num = 42; // Declare and initialize an integer variable

int *ptr = &num; // Declare and initialize a pointer to an integer with the address of 'num'

// Printing the values the values

printf("Value of num: %d\n", num);


printf("Value through pointer: %d\n", *ptr);
return 0;
}
7. Write a C program to find sum, mean and standard deviation of a list of numbers
using pointers along with an output.

#include<stdio.h>

#include<math.h>

int main()

float a[10], *ptr, mean, std, sum = 0, sumstd = 0;

int n, i;

printf("Enter the number of elements: ");

scanf("%d", &n);

printf("Enter array elements:\n");

for (i = 0; i < n; i++)

scanf("%f", &a[i]);

ptr = a;

for (i = 0; i < n; i++)

sum = sum + *ptr;

ptr++;

mean = sum / n;

ptr = a;

for (i = 0; i < n; i++)

sumstd = sumstd + pow((*ptr - mean), 2);

ptr++;

std = sqrt(sumstd / (n - 1));

printf("Sum=%.3f\t", sum);

printf("Mean=%.3f\t", mean);

printf("Standard Deviation=%.3f\n", std);

return 0;

}
8. a) What is a structure? Explain the declaration and initialization with an
example code.

Structure in C:

In C, a structure is a user-defined data type that allows you to group together variables of different data
types under a single name. It enables you to create a composite data type that can represent a collection of
related information.

Declaration and Initialization of Structure:

Declaration:
The syntax for declaring a structure in C is as follows:

struct structure_name {

data_type1 member1;

data_type2 member2;

// ... additional members

};

EXAMPLE
#include <stdio.h>

// Define the structure

struct Student {

char name[50];

int age;

float grade;

};

int main() {

// Declare and initialize a structure variable

struct Student student1 = {"John Doe", 20, 85.5};

// Access and print the values

printf("Student Information:\n");

printf("Name: %s\n", student1.name);

printf("Age: %d\n", student1.age);

printf("Grade: %.2f\n", student1.grade);

return 0;

}
b) Differentiate between structure and union.
9. Write a C program to read and display the details of employees (eNo, eName
and eSalary) using structure along with a neat output

#include <stdio.h>

struct Employee {

int eNo;

char eName[50];

float eSalary;

};

int main()

struct Employee employees[5];

printf("Enter details of employees:\n");

for (int i = 0; i < 5; i++)

printf("\nEnter details for Employee %d:\n", i + 1);

printf("**Employee Number: **");

scanf("%d", &employees[i].eNo);

printf("**Employee Name: **");

scanf("%s", employees[i].eName);

printf("Employee Salary: ");

scanf("%f", &employees[i].eSalary);

printf("\nDetails of employees:\n");

for (int i = 0; i < 5; i++)

printf("\nEmployee %d:\n", i + 1);

printf("Employee Number: %d\n", employees[i].eNo);

printf("Employee Name: %s\n", employees[i].eName);

printf("Employee Salary: %.2f\n", employees[i].eSalary);

return 0;

}
10. What is file? Explain the streams associated with a file with examples.
11 Illustrate the passing of structures through pointers using an example program with an output
12 Write note enumerated data type.
Enumerated Data Type in C:

An enumerated data type, often referred to as an enum, is a user-defined data type in C that consists of a set of
named integer constants. Enumerations provide a way to create symbolic names (enumerators) for integer values,
making the code more readable and less error-prone.

SYNTAX :
enum enum_name {

enumerator1,

enumerator2,

// ... additional enumerators


};

Key Points:

1. Enumerations improve code readability by providing meaningful names to integer values.


2. Enumerators are implicitly assigned integer values starting from 0, but you can explicitly assign
values if needed.
3. Enumerations are often used for representing sets of related constant values (e.g., days of the week,
colors, etc.).
4. Enumerations make the code more self-documenting and reduce the chances of using incorrect
integer values.

Enumerated data types are a powerful feature in C for creating clear and expressive code, especially when
dealing with a set of related constant values.

EXAMPLE
#include <stdio.h>

enum Color {

RED,

GREEN,

BLUE

};

int main()

enum Color myColor = GREEN;

switch (myColor) {

case RED:

printf("The color is Red.\n");

break;
case GREEN:

printf("The color is Green.\n");

break;

case BLUE:

printf("The color is Blue.\n");

break;

default:

printf("Invalid color.\n");

return 0;

You might also like