Comprehensive Guide to Computer Networks and C Programming with Loops and Arrays

Computer Network

Fill in the Blanks:

  1. The hostname command is used to display …………….
    Ans: Name of the computer.
  2. The length of an IPv4 and an IPv6 address is …………. and ………… bytes respectively.
    Ans: 32 bits and 128 bits.
  3. An access point is used to connect …………..
    Ans: Wireless network.
  4. The ping command is used to …………..
    Ans: Verify the connectivity between two computers.
  5. HTTPS transfers …………… data.
    Ans: Encrypted.

Multiple Choice Questions:

  1. Which device is required to connect multiple heterogeneous networks?
    a) hub
    b) Switch
    c) Router
    d) Access Point
    Ans: Router
  2. Which is the largest type of computer network?
    a) PAN
    b) LAN
    c) MAN
    d) WAN
    Ans: WAN
  3. Which protocol transfers encrypted data instead of plain data?
    a) HTTP
    b) HTTPS
    c) FTP
    d) SMTP
    Ans: HTTPS
  4. How many bytes are reserved for OUI in a MAC address?
    a) 3
    b) 8
    c) 24
    d) 12
    Ans: 24
  5. An IP address can be automatically assigned if the network is connected with a
    a) Access Point
    b) Mail Server
    c) Web Server
    d) DHCP Server
    Ans: DHCP Server

Short Answer Questions:

  1. Mention the name of components required to set up a MAN network. Draw a block diagram of a MAN network labeling the name of each component. (Hint: MAN network is a connection of multiple LANs)
    Ans. A MAN may use a local exchange carrier (LEC) to provide the connections between LANs and may connect to an internet exchange point for high-speed communication between the MAN and the public internet.
  2. Why should the hostname not be used to identify a computer in a computer network?
    Ans. Hostnames may not always be unique. There is a possibility that two or more computers in a computer network may have the same hostname. That is the reason why the hostname should not be used to identify a computer in a computer network.
  3. Give five examples of valid and five examples of invalid IPv4 addresses.
    Ans. A valid IPv4 address must be in the form of A.B.C.D, where A, B, C, and D are numbers from 0-255.
    Examples of valid IPv4 addresses are:
    ● 172.16.254.2
    ● 192.0.2.146
    ● 192.168.2.254
    ● 165.254.11.135
    ● 168.14.204.3
    Examples of invalid IPv4 addresses are:
    ● 0.168.120.26 (Any address that begins with 0 is invalid).
    ● 192.16.152.260 (Any address with a number above 255 in it is invalid).
    ● 192.16.152.250.12 (Any address that has more than 3 dots is invalid).
    ● 150.2.16.255 (The number 255 should not appear in any of the octets. The number 255 is reserved for broadcast addressing).
    ● 2001.162 (A valid IPv4 address must consist of 4 octets).
  4. Can we compare HTTP and FTP protocols? Mention their functionality.
    Ans. Comparison between HTTP and FTP:

    ParametersHTTPFTP
    Full-FormHyper Text Transfer ProtocolFile Transfer Protocol
    MeaningHTTP refers to a set of rules that determines the process of transfer of various web pages over various computers present on the internet.FTP refers to the set of rules that basically allows the process of uploading and downloading files from a computer to the internet.
    UsesWe use HTTP for providing various web pages from the web browser to the web server. The URLs that use the HTTP protocol begin with HTTP.We use FTP for downloading as well as uploading files between a server and a client over the internet. The URLs that use the FTP protocol begin with FTP.
  5. How many 16-port switches are required to connect 31 computers in a network?
    Ans. A 16-port switch can connect 16 computers. To connect 31 computers in a network, we need either a larger switch or 2 numbers of 16-port switches.

C Programming with Loops

1. Why do we use loops in C programs?

Ans: Loops are used to repeat the same code for a finite number of times.

2. Do we need to use only one type of loop in a C program? Justify your answer by writing a C program.

Answer: No, we can use all types of loops in a C program. The C program has three different types of loops —
• for loop
• do.. while loop
• while loop
A C program can be written using all three types of loops. For example, printing “Seba.com” 5 times using all three types of loops:

#include
int main() {
int n = 1, a;
while (n printf("Seba.com\n");
n++;
}
do {
printf("Seba.com\n");
n++;
} while (n for (a = 1; a printf("Seba.com\n");
}
return 0;
}

3. What will happen if we write a while loop with 1 in place of the condition? Try it in a simple C program. Hint: While (1) { Printf(“We must raise our voice against corruption \n”); }

Answer: If we write a while loop with 1 in place of the condition, the loop will execute forever as the condition will always remain true.
Code:-

#include
int main() {
int i = 1;
while (1) {
printf("We must raise our voice against corruption \n");
i++;
}
return 0;
}

4. Name different portions of a for loop. Can we put more than one statement within a portion?

Answer: The three portions of a for loop are –
• Initialization expression
• Condition/test expression
• Update expression
Yes, we can put more than one statement within a portion.

5. Answer with True or False.


i) If the condition of the while loop is false, the control comes to the second statement inside the loop. Answer: FALSE.
ii) We can use at most three loops in a single C program. Answer: FALSE.
iii) The statement inside the do-while loop executes at least once even if the condition is false. Answer: TRUE.
iv) Only the first statement inside the do-while loop executes when the condition is false. Answer: FALSE.
v) In a do-while loop, the condition is written at the end of the loop. Answer: TRUE.

6. Programming Exercises

A. Write a C program to find the summation of the following series.


(a). 1² + 2² + 3¹ + 4² + … + N²
Code:-

#include
#include
void main() {
int i, b, n, sum = 0;
clrscr();
printf("Enter the value of n =");
scanf("%d", &n);
for (int i = 1; i b = i * i;
sum = sum + b;
}
printf("\n The summation is = %d", sum);
getch();
}

(b). 1³ + 2³ + 3³ + 4³ + … + N³
Code:-

#include
#include
void main() {
int i, b, n, sum = 0;
clrscr();
printf("Enter the value of n =");
scanf("%d", &n);
for (int i = 1; i b = i * i * i;
sum = sum + b;
}
printf("\n The summation is = %d", sum);
getch();
}

(c). 1 * 2 + 2 * 3 + 3 * 4 + … + N * (N + 1)
Code:-

#include
#include
void main() {
int i, b, n, sum = 0;
clrscr();
printf("Enter the value of n =");
scanf("%d", &n);
for (int i = 1; i b = i * (i + 1);
sum = sum + b;
}
printf("\n The summation is = %d", sum);
getch();
}

B. Write a C program to continuously take a number as input and announce whether the number is odd or even. Hint: use a do-while loop

Code:-

#include
#include
void main() {
int a;
clrscr();
do {
printf("\n Enter Number:");
scanf("%d", &a);
if (a % 2 == 0)
printf("\n The number entered is even.");
else
printf("\n The number entered is odd.");
} while (a getch();
}

C. Write a C program to display the following pattern.

1
11
111
1111
11111
Code:-

#include
#include
void main() {
int j;
clrscr();
for (j = 1; j printf("%d", 1);
printf("\n");
for (j = 1; j printf("%d", 1);
}
printf("\n");
for (j = 1; j printf("%d", 1);
}
printf("\n");
for (j = 1; j printf("%d", 1);
}
printf("\n");
for (j = 1; j printf("%d", 1);
}
printf("\n");
getch();
}

D. Write a C program to display the following pattern.

5
54
543
5432
54321
Code:-

#include
#include
void main() {
int j;
clrscr();
for (j = 5; j >= 5; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 4; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 3; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 2; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 1; j--)
printf("%d", j);
printf("\n");
getch();
}

E. Write a C program to display the following pattern.

54321
5432
543
54
5
Code:-

#include
#include
void main() {
int j;
clrscr();
for (j = 5; j >= 1; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 2; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 3; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 4; j--)
printf("%d", j);
printf("\n");
for (j = 5; j >= 5; j--)
printf("%d", j);
printf("\n");
getch();
}

1. Write C programs to display the following patterns using a nested loop construct.


a.
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
Code:-

#include
#include
void main() {
int i, j;
clrscr();
for (i = 1; i for (j = 1; j printf("%d \t", j);
}
printf("\n");
}
getch();
}

b.
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
Code:-

#include
#include
void main() {
int i, j;
clrscr();
for (i = 1; i for (j = 1; j printf("%d\t ", j);
}
printf("1");
printf("\n");
}
getch();
}

c.
4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
4 3 2 1
Code:-

#include
#include
void main() {
int i, j;
clrscr();
for (i = 1; i for (j = 4; j >= 1; j--) {
printf("%d\t", j);
}
printf("\n");
}
getch();
}

d.
2
234
23456
Code:-

#include
#include
void main() {
int i, j, k;
clrscr();
for (i = 1; i for (j = 1; j printf(" ");
}
for (k = 1; k printf("%d", k + 1);
}
printf("\n");
}
getch();
}

e.
1
121
12321
Code:-

#include
#include
void main() {
int i, j;
clrscr();
for (i = 1; i for (j = 1; j printf(" ");
}
for (j = 1; j printf("%d", j);
}
for (j = i - 1; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
getch();
}

f.
*
***
*****
*******
*****
***
*
Code:-

#include
#include
void main() {
int i, j, k, p;
clrscr();
for (k = 1; k for (i = 1; i printf(" ");
}
for (j = 1; j printf("*");
}
for (p = 1; p printf("*");
}
printf("\n");
}
for (k = 3; k >= 1; k--) {
for (i = 1; i printf(" ");
}
for (j = 1; j printf("*");
}
printf("\n");
}
getch();
}

g.
X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X X X X X X X X X X X X X X X
Code:

#include
#include
void main() {
int i, j, k, p;
clrscr();
for (k = 1; k for (i = 1; i printf("X ");
}
for (j = 1; j printf(" ");
}
for (p = 1; p printf("X ");
}
printf("\n");
}
getch();
}

4. What is a nested loop? Why do we use nested loops in our programs?

Ans: Nesting of loops is the feature in C that allows the looping of statements inside another loop. The first loop is called the outer loop and the loop that appears inside another loop is called the inner loop.
Syntax of Nested loop:-

Outer_loop {
Inner_loop {
// inner loop statements.
}
// outer loop statements.
}

There can be more than one inner loop in a program. The nested loop construct simplifies programming in many cases. This also reduces the program length.

5. Do we need to use the same type of loop as outer and inner loops? Justify with some code segments.

Ans: No, we do not need to use the same type of loop as outer and inner loops. We do not need to use the same type of loop as outer and inner loops because the outer loop encloses the inner loop. So, the inner loop is a part of the outer loop and must start and finish within the body of the outer loop.
For example:

for (int i = 0; i // body of the outer for loop
for (int j = 0; j // body of the inner for loop
} // inner loop closed
// body of the outer for loop
} // outer loop closed

6. Can we put a third loop inside the inner loop of a nested loop construct? Write a C program to justify your answer.

Ans: Yes, we can put a third loop inside the inner loop of a nested loop construct.
For example:
X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X X X X X X X X X X X X X X X
To draw the above pattern we use the following code:-

#include
#include
void main() {
int i, j, k, p;
clrscr();
for (k = 1; k for (i = 1; i printf("X ");
}
for (j = 1; j printf(" ");
}
for (p = 1; p printf("X ");
}
printf("\n");
}
getch();
}

Arrays in C

1. Define Array? Why do we use arrays in computer programs?

Ans: An array is a collection of similar data items where elements are stored in contiguous memory locations in our computer. Each data item of an array is typically called an element and each element has a specific memory location and this can be accessed by its relative position in the array. This relative position can also be referred to as the index of the element.
We use arrays in computer programs because arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

2. Can we store both integer and float types of data in a single array? Demonstrate this by writing a simple C program?

Ans: No, we cannot store both integer and float types of data in a single array.

#include
#include
void main() {
int i, array[3];
clrscr();
printf("Enter the array element:");
for (i = 0; i scanf("%d", &array[i]);
printf("%d", array[i]);
printf(" ");
}
getch();
}

In the above program, when array elements are taken as integers, the program prints the correct output. But if we take the input as float values (like 3.14, 2.54, …) an error will occur. Hence, we cannot store both integer and float types of data in a single array.

3. Write the indices of the first and last element of the following array declaration?

Char city[7] = {‘S’, ‘I’, ‘L’, ‘C’, ‘H’, ‘A’, ‘R’};
Ans: The index of the first element i.e. S is 0 and the index of the last element i.e. R is 6.
ARRAY ELEMENT: S I L C H A R
INDICES: 0 1 2 3 4 5 6

4. Write a C program and declare an integer type array with capacity 7. Take input to the array from the keyboard. Display the 8th element of the array. Hint: display num[7] if num is the name of the array. Analyze the output.

Ans:

#include
#include
void main() {
int i, num[7];
clrscr();
printf("Enter the array elements:");
for (i = 0; i scanf("%d", &num[i]);
}
printf("The 8th array element is: ");
printf("%d", num[7]);
getch();
}

Output
Enter the array elements: 1 2 3 4 5 6 7
The 8th array element is: 344. Since the 8th element is not present in this array that’s why it’s showing a random value as output.

5. Write a C program and declare an integer type array with 7 elements in it. Display the address of the individual elements in the array.

Ans:

#include
#include
void main() {
int i, arr[7] = {1, 2, 3, 4, 5, 6, 7};
clrscr();
for (i = 0; i printf("The array element is %d ", arr[i]);
printf("and The address of the element is %p", &arr[i]);
printf("\n");
}
getch();
}

Output
The array element is 1 and the address of the element is 0x7ffe00b2099c
The array element is 2 and the address of the element is 0x7ffe00b209a0
The array element is 3 and the address of the element is 0x7ffe00b209a4
The array element is 4 and the address of the element is 0x7ffe00b209a8
The array element is 5 and the address of the element is 0x7ffe00b209ac
The array element is 6 and the address of the element is 0x7ffe00b209b0
The array element is 7 and the address of the element is 0x7ffe00b209b4

6. Write a C program and declare two integer type arrays, each with a capacity of 7. Take input only to the first array. Write a loop to copy the elements of the first array to the second one. Display the elements of the second array.

Ans:

#include
#include
void main() {
int i, num1[7], num2[7];
clrscr();
printf("Enter the array elements of the 1st array: -");
for (i = 0; i scanf("%d", &num1[i]);
}
for (i = 0; i num2[i] = num1[i];
}
printf("The array elements of the 2nd array copied from the 1st array are: -");
for (i = 0; i printf("%d", num2[i]);
printf(" ");
}
getch();
}

7. Write a strategy to find the summation of all the even numbers stored in an array. Write a C program for the same. If the array elements are {1,2,4,3,5,6,7,7,8}, the output of the program will be 20.

Ans: Strategy
Step1: Declare an integer array with sufficient capacity.
Int num[9];
Step2: Take input to the array elements.
Int num[9]={1,2,4,3,5,6,7,7,8};
Step3: Check whether the number is even or odd i.e. if the number is completely divisible by 2 then it is even.
If(num[i]%2==0)
Step4: if it is even calculate summation.
Sum=sum+num[i];
Step5: Declaration of the result.
Printf(“\n The summation is =%d”,sum);
Coding:-#include
#include
void main()
{
int i, sum=0, num[9]={1,2,4,3,5,6,7,7,8};
clrscr();
for(i=0;i
if(num[i]%2==0)
sum=sum+num[i];}
printf(“\n The summation is =%d”,sum);
getch(); }

8. Write a strategy to find the summation of all the even positioned numbers stored in an array. Write a C program for the same. If the array elements are (1,2,4,3,5,6,7,7,8}, the output of the program will be 18.
Ans: Strategy
Step1: Declare an integer array with sufficient capacity.
Int num[9];
Step2: Take input to the array elements.
Int num[9]={1,2,4,3,5,6,7,7,8};
Step3: Add all the even positioned numbers.
for(i=0;i
{
Sum=sum+num[i+1];
I++;
}
Step4: Declaration of the result.
Printf(“\n The summation is =%d”,sum);
Coding:-
#include
#include
void main()
{
int i, sum=0, num[9]={1,2,4,3,5,6,7,7,8};
clrscr();
for(i=0;i
sum=sum+num[i+1];
i++;
}
printf(“\n The summation is =%d”,sum);
getch();
}
9. Write the logic to replace only the first occurrence of an elements in an array. For example. If the array elements are {1,2,3,4,5,1,2,3), and we want to replace element 3 by 0.. the array content becomes {1,2,0,4,5,1,2,3}. Write a complete C program incorporating the logic.
Ans:
#include
#include
void main()
{
int i,j,k=8,m,n,num[8]={1,2,3,4,5,1,2,3};
clrscr();
printf(“The element of array are:-“);
for(i=0;i
printf(“%d”,num[i]);
printf(“ “);
}printf(“\n Replacing Element…….”);  printf(“\n Enter old number you want to replace:-“);   scanf(“%d”,&n);  printf(“\n Enter new number:-“);
scanf(“%d”,&m);  for( j=0;j
{  if(num[j]==n){ num[j]=m;
printf(“Number is replaced”); break;  }  }printf(“\n Now the element of array are:-“);  for(i=0;i
10. Write the logic to replace only the last occurrence of an elements in an array. For example. If the array elements are {1,2,3,4,5,1,2,3), and we want to replace element 3 by 0.. the array content becomes {1,2,3,4,5,1,2,0}. Write a complete C program incorporating the logic.
Ans:-
#include#includevoid main(){int i,j,k=8,m,n,num[8]={1,2,3,4,5,1,2,3};clrscr();printf(“The element of array are:-“);for(i=0;i
}printf(“\n Replacing Element…….”);printf(“\n Enter old number you want to replace:-“);scanf(“%d”,&n);printf(“\n Enter new number:-“);scanf(“%d”,&m);for( j=k-1;j>0;j++);{
if(num[j]==n){num[j]=m;printf(“Number is replaced”);break;} }printf(“\n Now the element of array are:-“);for(i=0;i
getch();
}
11. Write a C program to replace all the even positioned elements in an integer array by 0. For example, if the array elements are {1,2,3,9,5,5,7,1,9), it becomes {1,0,3,0,5,0,7,0,9}.
Ans:
#include
#include
void main()
{
int i,j,num[9],t,m,n;
clrscr();
printf(“Enter size of array:-“);
scanf(“%d”,&t);
printf(“Enter the elements of array:-);
for(i=0;i
scanf(“%d”,&num[i]);
printf(“ “);
}
printf(“\n The elements of array are:-“);
for(i=0;i
printf(“%d”,num[i]);
printf(“ “);
}
for(j=0;j
num[j+1]=0;
j++;
}
printf(“Number are replaced”);
printf(“Now the element of array are:-“);
for(i=0;i
printf(“%d”,num[i]);
printf(“ “);
}
getch();
}

12. Write a strategy to replace all the odd numbers in an integer array by 0. For example, if the array elements are {1,2,3,9,5,5,7,1,9}, it becomes {0,2,0,0,0,0,0,0,0}. Write a C program for the same.
Ans: #include
#include
void main()
{
int i,j,num[9],t,m,n;
clrscr();
printf(“Enter the number of elements to be entered in array:-“);
scanf(“%d”,&t);
printf(“Enter the elements of array:-);
for(i=0;i
scanf(“%d”,&num[i]);
printf(“ “);
}
printf(“\n The elements of array are:-“);
for(i=0;i
printf(“%d”,num[i]);
printf(“ “);
}
for(j=0;j
if(Num[j]%2!=0);
num[j]=0;
}
}
printf(“\n number is replaced”);
printf(“\n Now the element of array are:-”);
for(i=0;i
{
printf(“%d”,num[i]);
printf(“ “);
}
getch();
}
15. Write any two limitation of array? Can you store your name and roll number in the same array.
Ans: The two limitations of array are:-
1) Fixed size:- We have already seen that before using an array, we must declare it with a size. This is the capacity of the array. If some requirement arises (at run time)n to store more elements in that array, we cannot do so. For instance, in an integer array with size 10, we cannot store 11 integers.
2) Homogeneous data:- We know that an array is a collection of similar types of data. We cannot store a float variable in an integer array. No, we cannot store our name and roll number in same array because, an array can store only homogeneous data. As name is a string and roll number is an integer type data so we can’t store them in same array.