CP 3
CP 3
Eg:
union test {
Int x, y;
};
• A simultaneous equation refers to a set of equations that are solved together because
they share common variables. The goal is to find values for these variables that satisfy
all the equations in the system simultaneously.
For Example:
Linear simultaneous equations: The equations are linear, meaning each equation is of
the form ax+by+cz=dax + by + cz = dax+by+cz=d, where aaa, bbb, and ccc are
constants, and xxx, yyy, and zzz are the variables.
Non-linear simultaneous equations: The equations involve non-linear terms, such as
squares or higher powers of variables (e.g., x2+y2=1x^2 + y^2 = 1x2+y2=1).
Part B
16 marks:
11. a) i) Summarize various data types in C.
Data types:
Data type specifies which type of data that we are storing in a variable. There are 3 types of
data types.
⦁ Primary or primitive or fundamental data types
⦁ User defined data types
⦁ Derived data types
Integer:
'C' provides three different classes of integers.
They are
⦁ int
⦁ short int
⦁ long int
Floating point types:
Floating point or real numbers are stored in 32 bits (on all 16 bit and 32 bit machines) with 6
bits precision.
3 classes of floating point types:
⦁ float
⦁ double
⦁ long double
Character types:
A single character can be defined as a character type data.
ii) User defined data types:
'C' supports a feature known as “type definition” that allows users to define an identifier that
would represent an existing data type.
The user defined data type identifier can later be used to declare variables. General form:
typedef type identifier
where type is any existing data type, identifier is a new name given to the data type.
The Arithmetic operators performs arithmetic operations. The Arithmetic operators can
operate on any built in data type. A list of arithmetic operators are
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division and % Modulo division
2. Relational Operators
Relational Operators are used to compare arithmetic, logical and character expressions.
The Relational Operators compare their left hand side expression with their right hand
side expression. Then evaluates to an integer. If the Expressio is false it evaluate to
“zero”(0) if the expression is true it evaluate to “one”
Operator Meaning
< Less than
> Greater than
<= Less than or Equal to
>= Greater than or Equal to
= = Equal to
! = Not Equal to
3. Logical Operators
A logical operator is used to evaluate logical and relational expressions. The logical
operators act upon operands that are themselves logical expressions. There are three
logical operators.
Operators Expression
&&Logical AND
||Logical OR
!Logical NOT
4. Assignment Operator
An assignment operator is used to assign a value to a variable. The most commonly used
assignment operator is =. The general format for assignment operator is : <Identifer> = <
expression >
Syntax:
< operator >< variable name >;
The ++ or – operator can be used in the two
ways.
Example :
A bitwise operator operates on each bit of data. This bitwise operator can be divided into
three categories.
i. The logical bitwise operators.
ii. The shift operators
iii. The one’s complement operator.
8. Comma Operator
A set of expressions separated by using commas is a valid construction in c language.
Example :int i, j;
Definition:
A loop is defined as a block of statements which are repeatedly executed for certain
number of times.
Terms used in loop:
• loop variable
• initialization
• test condition
• incrimination/decrimination
Loop variable: It is a variable used in the loop.
Initialization: It is the first step in which starting and final values is assigned to the
loop variable. Each time the updated value is checked by the loop itself.
Test condition: It is used to test the condition.
Incrimination/Decrimination: It is the numerical value added to or subtracted from the
variable in each round of the loop.
Two ways of loop control structures:
• For loop
• While loop
• Do-while loop
For loop:
• For loop allows to execute a set of instructions repeatedly until the certain
becomes false.
• Assigning variable values, incrementation or decrementation and condition
checking is done in for statement only, where as other control structures are not
offered all these features in one statement.
Syntax:
Eg:
........
........
int i, sum=0, n ;
sum = sum + i;
........
........
#include<stdio.h>
#include<conio.h
> main ( )
{
int n, fact=1,i; clrscr ( );
printf ("enter the number for factorial=");
scanf ("%d", &n);
for (i=1; i<=n; i++)
{
fact = fact*i;
}
printf ("The factorial is=%d", fact); getch();
}
Output:
Enter the number for factorial=5
The factorial is=120
While loop:
The while is an entry-controlled loop statement. The test-condition is evaluated and if the
condition is true, then the body of the loop is executed. After execution of the body, the test-
condition is once again evaluated and if it is true, the body is executed once again. This
process of repeated execution of the body continues until the test-condition finally become
false and the control is transferred out of the loop. On exit, the program continues with the
statement immediately after the body of the loop.
Syntax:
Eg:
........
........
while ( i <= n)
........
Program: /* To find sum of individual digits */
#include<stdio.h>
#include<conio.h
> main()
int s,n=0,r;
clrscr();
scanf("%d",&s); while(s!=0)
r=s%10;
n=n+r; s=s/10;
Output:
Do-while loop:
Eg:
........
........
do
while ( i<=5)
........
........
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=0,b=1,c,s=0;
clrscr ( );
printf ( "Enter a value ");
scanf ("%d",&c);
printf ("%d %d ",a,b);
do
{
s=a+b;
a=b;
b=s;
printf (" %d ",s); s=a +b;
}
while (s<=c);
getch ( );
}
Output:
Enter a value 23 0 1 1 2 3 5 8 13 21
Output:
Enter the amount :100000
Enter the year 10
Enter the percentage:12
12)a) i) Interpret how to declare and accessing an array element with an example.
Definition: An array is a collection of similar data types in which each element is located
in separate sequence memory location.
Characteristics of array:
Declaration int a [5] ---- > creation of 5 variables of integer types in the memory.
Instead of declaring five variables for five values, the programmer can define them in an
array.
All the elements of an array share the same name, and they distinguished from one
another with the help of an element number.
The elements in an array plays major role for calling each element.
Eg: int a[5] = {1,3,5,7,9};
Calling array elements
a [0] refers to 1st element i.e. 1
a [1] refers to 2nd element i.e. 3
a [2] refers to 3rd element i.e. 5
a [3] refers to 4th element i.e. 7
a [4] refers to 5th element i.e. 9
Any particular element of an array can be modified separately without disturbing other
elements.
int a[5] = {1,3,5,7,9};
if a programmer needs to replace 5 with 10, programmer need not require to
change all other numbers expects 5. To carry out this task the statement a[2] = 10 can be
used. Here, the other elements are not disturbed.
void main() {
int a[3][3], b[3][3], c[3][3], i, j, k;
printf("Enter the elements for matrix 1:\n"); input(a);
printf("Enter the elements for matrix 2:\n");
input(b);
for(i = 0; i < 3; i++) { for(j
= 0; j < 3; j++) {
int sum = 0;
c[i][j] = sum;
}
}
printf("\n");
}
}
Output:
b)i) Define sorting. State the differences between linear search and binary search.
1. Ascending order: Items are arranged from the smallest to the largest, such as numbers
sorted from least to greatest or words sorted alphabetically from A to Z.
2. Descending order: Items are arranged from the largest to the smallest, such as
numbers sorted from greatest to least or words sorted alphabetically from Z to A.
ii) Write a C program to count the number of characters, spaces, vowels, constants and
other using string functions.
#include <stdio.h>
#include <string.h>
void main()
{
char a[20],b[20]; int
i,n,v=0,c=0,s=0;
printf("Enter the string:");
scanf("%[^\n]%*c",a);
n=strlen(a);
strcpy(b,a);
strlwr(b);
for(i=0;i<n;i++)
{
if(b[i]=='a'||b[i]=='e'||b[i]=='i'||b[i]=='o'||b[i]=='u')
v++;
else if(a[i]==' ')
s++;
else
c++;
}
printf("\nNo of length in the string :%d",n);
printf("\nNo of characters in the string :%d",n-s);
printf("\nNo of spaces in the string :%d",s);
printf("\nNo of vowels in the string :%d",v);
printf("\nNo of consonants in the string :%d",c);
printf("\nLower of the string :%s",b);
printf("\nUpper of the string :%s",strupr(b));
}
Output:
Enter the string: book
No of length in the string 4
No of characters in the string 4
No of spaces in the string 0
No of vowels in the string 2
No of consonants in the string 2
Lower of the string : book
Upper of the string : BOOK
int i=0,r=1;
while(i<e)
r*=b;
i++;
}
return r;
}
void main()
{
int b,e;
printf("Enter the base and exponent:");scanf("%d,%d",&b,&e);
printf("Power of the given number:%d",power(b,e));
}
Output :
Eg:
int num = 134;
where,
num ---- > Variable
134 ---- > Value
5000 ---- > Address
Since a pointer is a variable it is value is also stored the memory in another location. Suppose
we assign the address of quantity to a variable p.
P 5000 5048
In C/C++, a pointer is declared using the * operator, and it points to a variable of a specific
type:
int a = 10;
int* ptr = &a; // ptr holds the address of a
To access the value stored at the address the pointer is pointing to, the * operator
(dereference operator) is used
Pointer Arithmetic:
Pointer arithmetic involves manipulating the memory address stored in a pointer. You can
perform arithmetic operations like addition, subtraction, and comparison with pointers.
However, pointer arithmetic is done relative to the type the pointer is pointing to, rather than
directly on the memory address values.
Program:
#include<stdio.h>
int main ( )
{
int * p, sum, i;
int x [5] = { 5, 9, 6, 3, 7};
i= 0;
p = x;
printf(“Element Value Address /n /n “};
while (i <5)
{
printf(“ x [%d] %d %u /n”, i, *p, p);
i + +, p + +;
}
Output:
Element Value Address
X [0] 5 166
X [1] 9 168
X [2] 6 170
X [3] 3 172
X [4] 7 174
ii) Write a program to swap two numbers using call by value and call by reference.
Program:
#include <stdio.h>
void swap(int *a,int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
printf("\nAfter Swapping\na=%d\tb=%d",*a,*b);
}
void main()
{
int a,b;
printf("Enter the values of a and b:");scanf("%d,%d",&a,&b);
printf("Before Swapping\na=%d\t b=%d",a,b);
swap(&a,&b);
}
Output:
struct Person {
int age;
float height;
};
void printPersonInfo(struct Person p) {
printf("Age: %d, Height: %.1f\n", p.age, p.height);
// Modifying the structure inside the function
p.age = 30;
p.height = 6.0;
}
int main() {
struct Person person1 = {25, 5.9};
printPersonInfo(person1);
printf("After function call - Age: %d, Height: %.1f\n",
person1.age, person1.height);
return 0;
}
Explanation:
The structure person1 is passed to printPersonInfo() by value. This means that
the function works with a copy of person1, so modifications to p inside the function
do not affect the original person1.
struct Person {
int age;
float height;
};
void modifyPersonInfo(struct Person* p) {
p->age = 35;
p->height = 6.2;
}
int main() {
struct Person person1 = {25, 5.9};
modifyPersonInfo(&person1);
return 0;
}
Explanation:
The structure person1 is passed to modifyPersonInfo() by reference using a
pointer. The &person1 sends the memory address of person1, and the function can
modify the actual structure at that memory location. Inside the function, the arrow
operator (->) is used to access the structure's members and modify them.
ii) Create a structure called student that would contain name, regno and (10)
marks of five subjects and percentage. Write a C program to read the details of
name, regno and marks of five subjects for 30 students, calculate the percentage
and display the name, regno, marks of the subjects and percentage of each
student.
Program:
#include<stdio.h>
struct student
int n,i,j;
int sum=0;
s[i].percentage=(float)sum/5.00;
printf("\n");
}
for(i=0;i<n;i++)
{
printf("\n\nDetails of student%d",i+1);
printf("\nName :%sRegister Number:%ld",s[i].name,s[i].regno);
for(j=0;j<5;j++)
printf("\nMarks%d :%d",j+1,s[i].marks[j]);
printf("\nPercentage :%.2f%",s[i].percentage);
}
}
Output:
Details of student1
Name :Muthukumar
Register Number:2244035
Marks1 :99
Marks2 :99
Marks3 :99
Marks4 :99
Marks5 :99
Percentage :99.00
Details of student2
Name :Preethi
Register Number:2244041
Marks1 :100
Marks2 :100
Marks3 :100
Marks4 :100
Marks5 :100
Percentage :100.00
The array of structures can be simply manipulated as arrays of simple data types.
Example:
typedef struct
char name[20];
int age;
} person;
person class[5];
strcpy(class[0].name, “John”);
class[0].age = 19;
strcpy(class[1].name, “Sara”);
class[1].age = 18;
.name .age
class[0] John 19
class[1] Sara 18
class[2] David 20
class[3] Mary 21
class[4] Paul 18
Syntax:
To define an array of structures, first define the structure type, and then declare the
array of that type.
struct StructureName {
// Members of the structure
data_type member1;
data_type member2;
// Other members
};
// Declare an array of structures
struct StructureName arrayName[size];
Here, size represents the number of structures in the array.
Example:
#include <stdio.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare an array of 3 structures of type Person
struct Person people[3];
// Person 2
strcpy(people[1].name, "Bob");
people[1].age = 30;
people[1].height = 5.9;
// Person 3
strcpy(people[2].name, "Charlie");
people[2].age = 22;
people[2].height = 5.8;
return 0;
}
Output:
Person 1
Name: Alice
Age: 25
Height: 5.50
Person 2
Name: Bob
Age: 30
Height: 5.90
Person 3
Name: Charlie
Age: 22
Height: 5.80
#include <stdio.h>
int main()
FILE *fp;
char buffer[100];
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Reading content from the file:\n");
while (fgets(buffer, 100, fp) != NULL)
printf("%s", buffer);
}
fclose(fp); return
0;
}
Output:
#include <stdio.h>
#include <math.h>
}
float differentiate(float x,float h,float a,float b)
{
float e;
e=(a*(pow(h,b))-a*(pow(x,b)))/(h-x);
return e;
}
int main()
{
case 1:
{
s2+=differentiate(x,h,c[i],i);
}
printf("Invalid choice");
}
return 0;
}
Output 1:
Enter the degree of equation :3
Enter the x^3 coefficent :1
Enter the x^2 coefficent :2
Enter the x^1 coefficent :3
Enter the constant coefficent:4
Entered f(x)=1x^3+2x^2+3x+4
1.Numerical Integration
2.Numerical differentiation
Enter your choice:1
Enter the upper limit:1
Enter the lower limit:0
6.08
Output 2:
Enter the degree of equation :1 3
Enter the x^3 coefficent :1
Enter the x^2 coefficent :2
Enter the x^1 coefficent :3
Enter the constant coefficent:4
Entered f(x)=1x^3+2x^2+3x+4
1.Numerical Integration
2.Numerical differentiation
Enter your choice:2
Enter the point 1:1
Enter the point 2:2
Numerical differentiation for the given function is 16.00
#include <stdio.h>
int main() {
// Declare arrays for y1, y2, and t (time)
double y1[STEPS], y2[STEPS], t[STEPS];
Output:
Time Y1 Y2
0.00 1.0000 0.0000
0.01 1.0000 0.0100
0.02 0.9999 0.0200
...
9.99 -0.1481 -0.1480