Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views24 pages

Computer Science Assignment

Uploaded by

tarikusamson77
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)
23 views24 pages

Computer Science Assignment

Uploaded by

tarikusamson77
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/ 24

1)The von Neumann architecture is a fundamental design for

modern computers. It was proposed by John von Neumann in


1945 and it's also known as the von Neumann model or Princeton
architecture. This architecture describes a computer with a single
memory that stores both instructions (programs) and data. It has
four main components: the Arithmetic Logic Unit (ALU), the
Control Unit (CU), the Memory, and the Input/Output (I/O)
devices.
1. The Arithmetic Logic Unit (ALU): This is the part of the
processor where calculations are performed. It can add,
subtract, multiply, divide, and perform logical operations like
AND, OR, NOT, etc.
2. The Control Unit (CU): This unit controls the operation of the
entire computer. It fetches the instructions from memory,
decodes them, and executes them.
3. The Memory: This is where both instructions and data are
stored. It can be divided into two parts: the cache memory
and the main memory (RAM).
4. The Input/Output (I/O) devices: These are the devices that
allow the computer to interact with the outside world.
Examples include the keyboard, mouse, monitor, printer, etc.
The von Neumann architecture is sequential, meaning that it
processes one instruction at a time. This can lead to a bottleneck,
as the processor must wait for the memory to provide the next
instruction. This is known as the von Neumann bottleneck.
Despite this, the von Neumann architecture is the basis for most
modern computer systems.
Now, let's move on to the basic structure of a C program. A C
program consists of the following parts:
1. Preprocessor Commands: These are lines that start with
a # symbol. They are not actually part of the program but are
directives for the compiler.
2. Functions: A C program is a collection of functions. A
function is a block of code that performs a speci c task.
The main() function is the entry point of the program.
fi
3. Variables: These are used to store data. In C, you can
declare a variable like this: int count; This declares a variable
named count that can store integer values.
4. Constants: These are values that cannot be changed during
the execution of the program. In C, you can declare a
constant like this: const int DAYS_IN_WEEK = 7;
5. Data Types: C supports several data types,
including int (integer), oat ( oating point
number), char (character), double (double precision oating
point number), etc.

2)Data representation in computers is a crucial aspect of


understanding how information is stored and processed in
digital systems. This includes various number systems,
character representation codes, and their interconversions,
as well as binary and oating-point arithmetic, and signed
and unsigned numbers.
Number Systems: Computers primarily use the binary (base-2)
number system for internal operations. However, other number
systems are also essential for understanding and representing
data. Here are some common number systems and their
conversions:
• Decimal (base-10): The decimal number system is the most
commonly used system in daily life. It has 10 symbols (0-9).
• Binary (base-2): The binary number system is used in
computers and consists of two symbols (0 and 1).
• Octal (base-8): The octal number system uses eight symbols
(0-7). It is used for compact representation of binary
numbers.
• Hexadecimal (base-16): The hexadecimal number system
uses 16 symbols (0-9 and A-F). It is used for compact
representation of binary numbers in programming
languages.
fl
fl
fl
fl
Character Representation Codes: Character representation codes
map characters (letters, digits, symbols, etc.) to numerical values.
Some common character representation codes are:
• ASCII (American Standard Code for Information
Interchange): A 7-bit or 8-bit code used to represent
characters in the English alphabet.
• Unicode: A 16-bit or 32-bit code used to represent characters
in multiple languages and symbols.
Interconversions: Interconversions involve changing the
representation of data from one form to another. Examples
include converting decimal numbers to binary, binary to
hexadecimal, or ASCII to Unicode.
Binary Arithmetic: Binary arithmetic is the process of performing
mathematical operations using binary numbers. The basic binary
arithmetic operations are addition, subtraction, multiplication, and
division.
Floating-Point Arithmetic: Floating-point arithmetic is the process
of performing mathematical operations using oating-point
numbers. These are represented in the IEEE 754 standard, which
includes single-precision (32-bit) and double-precision (64-bit)
formats.
Signed and Unsigned Numbers: Signed numbers are those that
can be positive, negative, or zero. Unsigned numbers are those
that can only be positive or zero. In binary representation, the
most signi cant bit (MSB) is used to represent the sign. For
signed numbers, the MSB is 0 for positive and 1 for negative
numbers. For unsigned numbers, the MSB is always 0.

3)a) Relational and Logical operators:


Relational operators are used to compare two values or variables
and determine their relationship. The result of a relational
operation is either 1 (true) or 0 (false). Here are some examples:
fi
fl
1#include <stdio.h>
2int main() {
3 int a = 5, b = 10;
4 printf("a > b is %d \n", a > b);
5 printf("a < b is %d \n", a < b);
6 printf("a == b is %d \n", a == b);
7 printf("a != b is %d \n", a != b);
8 printf("a >= b is %d \n", a >= b);
9 printf("a <= b is %d \n", a <= b);
10 return 0;
11}
Output:

1a > b is 0
2a < b is 1
3a == b is 0
4a != b is 1
5a >= b is 0
6a <= b is 1
Logical operators are used to combine two or more relational
expressions and determine if they are true or false. Here are
some examples:

1#include <stdio.h>
2int main() {
3 int a = 5, b = 10, c = 20;
4 printf("(a > b) && (b > c) is %d \n", (a > b) && (b > c));
5 printf("(a < b) || (b < c) is %d \n", (a < b) || (b < c));
6 printf("!(a > b) is %d \n", !(a > b));
7 return 0;
8}
Output:
1(a > b) && (b > c) is 0
2(a < b) || (b < c) is 1
3!(a > b) is 1
b) Int main() and Void main():
The difference between int main() and void main() lies in the
return type of the main function. int main() returns an integer
value to the operating system after the program has nished
executing, while void main() does not return any value. Here are
some examples:
c

// int main()
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}

#include <stdio.h>
void main() {
printf("Hello World!\n");
// No return statement required
}
c) Variable and constant:
Variables are used to store values that can change during the
execution of a program, while constants are used to store values
that cannot be changed. Here is an example:

#include <stdio.h>
int main() {
int variable = 5; // Variable
const int constant = 10;
printf("Variable value: %d \n", variable);
fi
printf("Constant value: %d \n", constant);
variable = 15;

printf("Variable value after change: %d \n", variable);


return 0;
}

4)Arithmetic Operators: Arithmetic operators perform mathematical


operations such as addition, subtraction, multiplication,
division, modulus, and unary minus. Here's an example:

1#include <stdio.h>
2int main()
3{
4 int a = 5, b = 10, c;
5
6 c = a + b;
7 printf("a + b = %d \n", c);
8 c = a - b;
9 printf("a - b = %d \n", c);
10 c = a * b;
11 printf("a * b = %d \n", c);
12 c = b / a;
13 printf("b / a = %d \n", c);
14 c = b % a;
15 printf("Remainder when b divided by a = %d \n", c);
16 c = -a;
17 printf("Unary minus of a = %d \n", c);
18
19 return 0;
20}
Output:
Relational Operators: Relational operators are used to compare
two values. They return a boolean value indicating whether the
comparison is true or false. Here's an example:

1#include <stdio.h>
2int main()
3{
4 int a = 5, b = 10;
5
6 printf("5 == 5 is %d \n", a == b);
7 printf("5 == 10 is %d \n", a == b);
8 printf("5 != 10 is %d \n", a != b);
9 printf("5 < 10 is %d \n", a < b);
10 printf("5 > 10 is %d \n", a > b);
11 printf("5 <= 10 is %d \n", a <= b);
12 printf("5 >= 10 is %d \n", a >= b);
13
14 return 0;
15}

5 a)Conditional execution in C is achieved using if, else, and


nested if statements. The if statement allows you to execute
a block of code if a certain condition is true. The else
statement lets you specify a block of code to be executed if
the condition in the if statement is false. Nested if statements
allow you to include if statements inside an if or else block,
creating more complex conditional logic. Here's an example:

1#include <stdio.h>
2
3int main() {
4 int score;
5
6 printf("Enter your score: ");
7 scanf("%d", &score);
8
9 if (score >= 90) {
10 printf("Grade A\n");
11 } else if (score >= 80) {
12 printf("Grade B\n");
13 } else if (score >= 70) {
14 printf("Grade C\n");
15 } else if (score >= 60) {
16 printf("Grade D\n");
17 } else {
18 printf("Grade F\n");
19 }
20
21 return 0;
22}
For example , the user enters a score, and the program checks if
the score is greater than or equal to 90, 80, 70, 60, and nally, if
none of the conditions are met, it defaults to grade F.
The switch statement is an alternative to the if-else-if ladder. It is
used when you want to execute a block of code based on the
value of a single variable or expression. example:

1#include <stdio.h>
2
3int main() {
4 int day;
5
6 printf("Enter a number from 1 to 7: ");
7 scanf("%d", &day);
8
9 switch (day) {
10 case 1:
11 printf("Monday\n");
12 break;
13 case 2:
14 printf("Tuesday\n");
15 break;
16 case 3:
17 printf("Wednesday\n");
18 break;
fi
19 case 4:
20 printf("Thursday\n");
21 break;
22 case 5:
23 printf("Friday\n");
24 break;
25 case 6:
26 printf("Saturday\n");
27 break;
28 case 7:
29 printf("Sunday\n");
30 break;
31 default:
32 printf("Invalid day number\n");
33 break;
34 }
35
36 return 0;
37}
In this example, the user enters a number from 1 to 7, and the
program checks the value of the variable 'day' and prints the
corresponding day of the week.
Recursion is a programming technique where a function calls
itself repeatedly until it meets a certain condition. Here's an
example of a recursive function in C that calculates the factorial of
a number

1#include <stdio.h>

3int factorial(int n);


4
5int main() {
6 int num;
7
8 printf("Enter a positive integer: ");
9 scanf("%d", &num);
10
11 printf("Factorial of %d is: %d\n", num, factorial(num));
12
13 return 0;
14}
15
16int factorial(int n) {
17 if (n == 0) {
18 return 1;
19 } else {
20 return n * factorial(n - 1);
21 }
22}
In this example, the factorial function calls itself repeatedly,
decrementing the value of 'n' until it reaches 0, at which point it
returns 1, and the recursion stops. The nal result is the factorial
of the number entered by the user.

5b)
A recursive function is a function in C that calls itself repeatedly
until it meets a certain condition. Recursion is a powerful
programming technique that simpli es the solution to complex
problems. Here's an example of a recursive function in C that
calculates the factorial of a number entered by the user:

1#include <stdio.h>
2
3int factorial(int n);
4
5int main() {
6 int num;
7
8 printf("Enter a positive integer: ");
9 scanf("%d", &num);
10
11 printf("Factorial of %d is: %d\n", num, factorial(num));
12
13 return 0;
14}
15
16int factorial(int n) {
17 if (n == 0) {
fi
fi
18 return 1;
19 } else {
20 return n * factorial(n - 1);
21 }
22}
In this example, the factorial function calls itself repeatedly,
decrementing the value of 'n' until it reaches 0, at which point it
returns 1, and the recursion stops. The nal result is the factorial
of the number entered by the user.
The factorial function uses an if-else statement to check the value
of 'n'. If 'n' is 0, the function returns 1. Otherwise, it calls itself with
the argument n-1 and multiplies the result by 'n'. This process
continues until 'n' is 0, at which point the recursion stops and the
nal result is returned.
The main function prompts the user to enter a positive integer,
reads the input, and calls the factorial function to calculate the
factorial of the entered number. The result is then printed to the
console.

6a)A structure is a user-de ned data type that allows you to


combine data items of different kinds. Structures are used to
represent a record. A record is a collection of related data items.
For example, you might use a structure to represent a student
with elds for the student's name, ID number, and GPA.
example:

1struct Student {
2 char name[50];
3 int id;
4 oat gpa;
5};
To initialize a structure variable,
c
fi
fl
fi
fi
fi
1struct Student s1;
2s1.id = 12345;
3strcpy(s1.name, "John Smith");
4s1.gpa = 3.5;
Alternatively, you can initialize a structure variable at the time of
declaration:

1struct Student s1 = {"John Smith", 12345, 3.5};


To manipulate structure members, you can use the dot operator:

1printf("Student name: %s\n", s1.name).


2printf("Student ID: %d\n", s1.id);
3printf("Student GPA: %.2f\n", s

6b)A pointer in C is a variable that stores the memory address of another


variable. Pointers are used for various purposes in C, such as dynamic
memory allocation, array manipulation, and function parameters.

1#include <iostream>
2using namespace std;
3int main()
4{
5 int* largest;
6 int a[5]={4,5,666,7,8};
7 largest=a;
8 for(int i=1; i<5; i++)
9 {
10 if(*largest < a[i])
11 {
12 largest = &a[i];
13 }
14 }
15 cout<<*largest;
16}
1#include <iostream>
2)Int main(){
4 int arr[] = {5, 3, 8, 1, 9};
5 int* largest = arr; array
6 int* current;
7
8 for (current = arr + 1; current < arr + 5; current++) {
9 if (*current > *largest) {
10 largest = current;
11 }
12 }
13
14 printf("The largest number in the array is %d\n", *largest);
15
16 return 0;
17}

7a)An array in C is a collection of elements of the same data type stored


in contiguous memory locations. Elements of an array can be accessed
using the array name and the index number as the location in memory.
There are two types of arrays in C: one-dimensional arrays and
two-dimensional arrays.
One-dimensional arrays, also known as single-dimensional
arrays, are used to store a collection of elements of the same
data type in a single row. example of a one-dimensional array:

1int arr[5]; // an array with one row and ve columns will be created.
You can initialize a one-dimensional array at the time of
declaration or explicitly at runtime. example of initializing a one-
dimensional array at the time of declaration:

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


example of initializing a one-dimensional array at runtime:
fi
2arr
1int arr[5];
2arr[0] = 1;
3arr[1] = 2;
4arr[2] = 3;
5arr[3] = 4;
6arr[4] = 5;

You can iterate over a one-dimensional array using a for loop.


example of a program that calculates the sum of the elements in a
one-dimensional array:

1#include <stdio.h>
2
3int main() {
4 int arr[5] = {1, 2, 3, 4, 5};
5 int sum = 0;
6 for(int i = 0; i < 5; i++) {
7 sum += arr[i];
8 }
9 printf("The sum is: %d\n", sum);
10 return 0;
11}
Two-dimensional arrays, also known as multi-dimensional arrays,
are used to store a collection of elements of the same data type in
a table format. Here's an example of a two-dimensional array:

Edit
1int arr[2][5];
You can initialize a two-dimensional array at the time of
declaration or explicitly at runtime. example of initializing a two-
dimensional array at the time of declaration:
1int arr[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
example of initializing a two-dimensional array at runtime

1int arr[2][5];
2arr[0][0] = 1;
3arr[0][1] = 2;
4arr[0][2] = 3;
5arr[0][3] = 4;
6arr[0][4] = 5;
7arr[1][0] = 6;
8arr[1][1] = 7;
9arr[1][2] = 8;
10arr[1][3] = 9;
11arr[1][4] = 10

7b)#include <stdio.h>
2
3int main() {
4 int m, n, i, j;
5 printf("Enter the number of rows and columns of the matrices: ");
6 scanf("%d %d", &m, &n);
7
8 double mat1[m][n];
9 printf("\nEnter the elements of the rst matrix:\n");
10 for (i = 0; i < m; i++) {
11 for (j = 0; j < n; j++) {
12 printf("Enter element a%d%d: ", i + 1, j + 1);
13 scanf("%lf", &mat1[i][j]);
14 }
15 }
16
17 double mat2[m][n];
18 printf("\nEnter the elements of the second matrix:\n");
19 for (i = 0; i < m; i++) {
20 for (j = 0; j < n; j++) {
21 printf("Enter element b%d%d: ", i + 1, j + 1);
fi
22 scanf("%lf", &mat2[i][j]);
23 }
24 }
25
26 double sum[m][n];
27 for (i = 0; i < m; i++) {
28 for (j = 0; j < n; j++) {
29 sum[i][j] = mat1[i][j] + mat2[i][j];
30 }
31 }
32
33 printf("\nThe sum of the matrices is:\n");
34 for (i = 0; i < m; i++) {
35 for (j = 0; j < n; j++) {
36 printf("%.2lf\t", sum[i][j]);
37 }
38 printf("\n");
39 }
40
41 return 0;
42}

8) switch case
The switch statement is a control statement in C that allows you to
execute different blocks of code based on the value of an
expression. It is often used as an alternative to the if-else
statement for making decisions based on a set of discrete values.
Here is an example program that prints the day of the week based
on user input using a switch statement:
1#include <stdio.h>
2
3int main() {
4 int day;
6 printf("Enter a number for a day of the week (1-7): ");
7 scanf("%d", &day);
8
9 switch (day) {
10 case 1:
11 printf("Sunday\n");
12 break;
13 case 2:
14 printf("Monday\n");
15 break;
16 case 3:
17 printf("Tuesday\n");
18 break;
19 case 4:
20 printf("Wednesday\n");
21 break;
22 case 5:
23 printf("Thursday\n");
24 break;
25 case 6:
26 printf("Friday\n");
27 break;
28 case 7:
29 printf("Saturday\n");
30 break;
31 default:
32 printf("Invalid input! Please enter a number between 1 and
7.\n");
33 }
34
35 return 0;
36}
9a)Create a structure called "Student" with members name, age, and
total marks. Write a C program to input data for two students, display
their information, and nd the average of total marks.
fi
1struct Student { 5 char name[50];
2 int age;
7 oat totalMarks;
8};

10int main() {
11
12 struct Student student1, student2;
15 printf("Input details for Student 1:\n");
16 printf("Name: ");
17 scanf("%s", student1 :\n”);
18 printf("Age: ");
19 scanf("%d", &student1.age);
20 printf("Total Marks: ");
21 scanf("%f", &student1.totalMarks);
22

24 printf("\nInput details for Student 2:\n");


25 printf("Name: ");
26 scanf("%s", student2.name);
27 printf("Age: ");
28 scanf("%d", &student2.age);
29 printf("Total Marks: ");
30 scanf("%f", &student2.totalMarks);
31

33 printf("\nStudent 1 Information:\n");
34 printf("Name: %s\n", student1.name);
35 printf("Age: %d\n", student1.age);
36 printf("Total Marks: %.2f\n", student1.totalMarks);
fl
37
38 printf("\nStudent 2 Information:\n");
39 printf("Name: %s\n", student2.name);
40 printf("Age: %d\n", student2.age);
41 printf("Total Marks: %.2f\n", student2.totalMarks);
42 oat averageMarks = (student1.totalMarks + student2.totalMarks) / 2;
43 printf("\nAverage Total Marks: %.2f\n", averageMarks);
44return 0;

9b)Write a program in C to nd the second largest element in an array.


#include <stdio.h>

4void secondLargest(int arr[], int n) {


5 int i, rst, second;
6 if (n < 2) {
7 printf("Invalid array size!\n");
8 return;
9 }
10 rst = second = arr[0];
11 for (i = 1; i < n; i++) {
12 if (arr[i] > rst) {
13 second = rst;
14 rst = arr[i];
15 } else if (arr[i] > second && arr[i] != rst) {
16 second = arr[i];
17 }
18 }
19 printf("The second largest element is %d\n", second);
20}
22int main() {
23 int arr[] = {12, 34, 10, 6, 40};
24 int n = sizeof(arr) / sizeof(arr[0]);
25 secondLargest(arr, n);
26 return 0;
27}
fl
fi
fi
fi
fi
fi
fi
fi
c)Write a program to print bonacci series up to 100.

#include <stdio.h>
2
3// Function to nd the second largest element in an array
4void secondLargest(int arr[], int n) {
5 int i, rst, second;
6 if (n < 2) {
7 printf("Invalid array size!\n");
8 return;
9 }
10 rst = second = arr[0];
11 for (i = 1; i < n; i++) {
12 if (arr[i] > rst) {
13 second = rst;
14 rst = arr[i];
15 } else if (arr[i] > second && arr[i] != rst) {
16 second = arr[i];
17 }
18 }
19 printf("The second largest element is %d\n", second);
20}
21
22int main() {
23 int arr[] = {12, 34, 10, 6, 40};
24 int n = sizeof(arr) / sizeof(arr[0]);
25 secondLargest(arr, n);
26 return 0;
27}
fi
fi
fi
fi
fi
fi
fi
fi
d)Explain fopen, fread, fwrite, fseek

• fopen: This function is used to open a le in a speci ed


mode. The mode can be "r" for reading, "w" for writing, "a"
for appending, and "rb", "wb", and "ab" for binary mode. The
function returns a le pointer that can be used in
subsequent le operations.
• fread: This function is used to read data from a le. It takes
four arguments: a pointer to a buffer where the read data will
be stored, the size of each element in the buffer, the number
of elements to read, and a le pointer pointing to the le to
read from. The function returns the number of elements
actually read.
• fwrite: This function is used to write data to a le. It takes
four arguments: a pointer to a buffer containing the data to
write, the size of each element in the buffer, the number of
elements to write, and a le pointer pointing to the le to
write to. The function returns the number of elements
actually written.
• fseek: This function is used to change the le position
indicator of a le. It takes three arguments: a le pointer
pointing to the le, the offset from the new position, and a
ag indicating the type of offset. The ag can
be SEEK_SET (from the beginning of the
le), SEEK_CUR (from the current position),
or SEEK_END (from the end of the le).
fl
fi
fi
fi
fi
fi
fi
fi
fi
fl
fi
fi
fi
fi
fi
fi
fi
fi
10 a) What is Command Line Argument? Explain with Example.

A command line argument is a parameter that is passed to a program when it is


executed from the command line interface of an operating system. The values of these
arguments are passed on to your program during program execution. In C
programming, command line arguments are handled by the main() function of a C
program. The main() function has two parameters: the rst parameter is the number of
command-line arguments and the second is a list of command-line arguments.

example of a C program that prints the command line arguments

1#include <stdio.h>
2
3int main(int argc, char *argv[]) {
4 int i;
5 printf("Number of arguments: %d\n", argc);
6 for (i = 0; i < argc; i++)
7 printf("Argument %d: %s\n", i, argv[i]);
8 return 0;
9}

b)continue and breakare control statements used in loops to alter


the flow of loop execution.
The continue statement is used to skip the current iteration of a
loop and move on to the next iteration. When
the continue statement is encountered inside a loop, the current
iteration is terminated and the control returns to the beginning of
the loop, where the condition is checked again. example:

1#include <stdio.h>
2
3int main() {
4 int i;
fi
5 for(i = 1; i <= 10; i++) {
6 if(i % 2 == 0)
7 continue;
8 printf("%d ", i);
9 }
10 return 0;
11}

What is function prototype? Explain with example.

C)a function prototype is a declaration of a function that specifies its


return type, function name, and the types and order of its parameters.
It is used to inform the compiler about the function's interface, so
that the function can be called from other parts of the program.
The function prototype has the following format:
return_type function_name(parameter_list);
Here's an example of a function prototype:
int add(int a, int b);
This function prototype declares that the add function takes two
integers as arguments and returns an integer value.
A function prototype can be placed in a header le, or it can be
included in the same le as the function de nition. If the function
prototype is not provided, the compiler assumes that the function
returns an integer value. However, it is considered good
programming practice to always provide a function prototype.
Here's an example of a program that uses a function prototype:

1#include <stdio.h>
2
3// Function prototype
4int add(int a, int b);
5
fi
fi
fi
6int main() {
7 int x = 10, y = 20, sum;
8 sum = add(x, y);
9 printf("The sum is: %d\n", sum);
10 return 0;
11}
12
13// Function definition
14int add(int a, int b) {
15 return a + b;
16}
D)Differentiate between Structure and Union.
A structure is a collection of variables of different data types, where each variable has
its own memory allocation. This means that all members of a structure get their own
block of memory.
On the other hand, a union is a data type in C programming that allows storing different
data types in the same memory location. It provides an ef cient way of using the same
memory location for multiple purposes. However, only one member can contain a value
at any given time.

|
Perform the following

A) (3041.125)10=(5741.1)8= (be1.2)16=(101111100001.001)2
B) (A010D)16=(50015)8=(655629)10=(101000000001101)2
fi

You might also like