Professional Documents
Culture Documents
variable initialization;
while (condition to control loop) {
statement 1;
statement 2;
..
..
iteration of variable;
}
In the pseudo code above :
Variable initialization is the initialization of counter of loop before start of while loop
Condition is any logical condition that controls the number of times execution of loop statements
#include <stdio.h>
int main () {
int i = 0;
int loop_count = 5;
printf("Case1:\n");
while (i<loop_count) {
printf("%d\n",i);
i++;
}
printf("Case2:\n");
i=20;
while (0) {
printf("%d\n",i);
i++;
}
printf("Case3:\n");
i=0;
while (i++<5) {
printf("%d\n",i);
}
printf("Case4:\n");
i=3;
while (i < 5 && i >=2) {
printf("%d\n",i);
i++;
}
return 0;
}
2. Given a number, write a program using while loop to reverse the digits of
the number ?
Ans:#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
long n,j;
clrscr();
printf(Enter the No :- );
scanf(%ld,&n);
printf(\n);
printf(Reverce of the number is.);
while(n>0)
{
j = n%10;
printf(%ld,j);
n /= 10;
}
getch();
}
/*
********
OUTPUT
********
Enter the No :- 23456
Reverce of the number is.65432
*/
3.Write a program to compute the sum of the digits of a given integer number.
Ans:#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,num,x,sum=0;
printf("Enter a number=");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
getch();
}
4.List out the similarities and differences between structures and Unions.?
Ans:-
Structure
1.The keyword struct is used to define a structure
2. When a variable is associated with a structure,
the compiler allocates the memory for each
member. The size of structure is greater than or
equal to the sum of sizes of its members. The
smaller members may end with unused slack
bytes.
3. Each member within a structure is assigned
unique storage area of location.
4. The address of each member will be in
ascending order This indicates that memory for
Union
1. The keyword union is used to define a union.
2. When a variable is associated with a union,
the compiler allocates the memory by
considering the sizeof the largest memory. So, size
of union is equal to the size of largest member.
Note that the type of variable above should be same as the pointer type.(Though this is not a
strict rule but for beginners this should be kept in mind).
char ch = 'c';
char *chptr;
chptr = &ch //initialize
7.
Ans:#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*temp;
void insert_data(int value)
{
struct node *var;
temp=head;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
var->next=NULL;
temp->next=var;
}
}
int count_node()
{
int i=0;
temp=head;
while(temp!=NULL)
{
i++;
temp=temp->next;
}
printf("\n\nnumber of nodes are %d ",i);
}
void display()
{
struct node *var;
var=head;
printf("\nlist of elments are \n");
while(var!=NULL)
{
printf("-> %d ",var->data);
var=var->next;
}
}
int main()
{
int i,value;
char ch='y';
head=NULL;
printf(" 1.) Insert node");
printf("\n 2.) display the list");
printf("\n 3.) count number of nodes");
printf("\n 4.) exit");
while(ch=='y')
{
printf("\nChoose to do operation :");
scanf("%d",&i);
switch(i)
{
case 1 :
{
printf("\nEnter the data to be inserted in node ");
scanf("%d",&value);
insert_data(value);
//display();
break;
}
case 2 :
{
display();
break;
}
case 3 :
{
count_node();
break;
}
case 4 :
{
exit(0);
break;
}
}
}
getch();
}
this
matrix the entry in the ith row and jth column is 1 if there is an edge from the ith vertex to
the jth vertex in the graph G. If there is no such edge, then the entry will be zero. It is to be
noted that
(i) The adjacency matrix of a undirected graph is always symmetric, i.e., M [i,j] = M [j, i]
(ii) The adjacency matrix for a directed graph need not be symmetric.
(iii) The memory requirement of an adjacency matrix is n2 bits
For example for the graph in the following figure (a) is adjacency matrix is given in (b)
1
Figure. 4 (a)
1
0
1
1
0
0
1
0
1
0
0
3
0
1
0
1
1
4
1
0
1
0
1
5
0
1
1
1
0
(i) Suppose if we want to know how much time will take in finding number of edges a graph
with n vertices?
a
e
Since the space needed to represent a graph is n2 bits where n is a number of vertices. All
algorithm will require at least 0 (n2) time because n2 n entries of the matrix have to be
examined. Diagonal entries are zero.
s
m
B
e
(ii) Suppose the most of the entries in the adjacency matrix are zeros, i.e., when a graph is a
sparse... How much time is needed to the find m number of edges in a graph? It will take
much less time if say 0 (e + n), where e is the number of edges is a graph and e << n2/2.
But this can be achieved if a graph is represented through an adjacency list where only h
the edges will be represented
int i, j, k;
begin
for i :=1 to n do
for j := 1 to n do
P[i, j] := A[i, j];
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
P[i, j] := P[i, j] or (P[i, k]
and P[k, j])
end;
Implementation
function findIndex(values, target) {
for(var i = 0; i < values.length; ++i){
}
/* Driver program to check above functions */
int main()
{
int arr[] = {1, 3, 50, 10, 9, 7, 6};
int n = sizeof(arr)/sizeof(arr[0]);
printf("The maximum element is %d", findMaximum(arr, 0, n-1));
getchar();
return 0;
}
Time Complexity: O(Logn)
This method works only for distinct numbers. For example, it will not work for an array like {0, 1, 1, 2, 2, 2,
2, 2, 3, 4, 4, 5, 3, 3, 2, 2, 1, 1}.
11. Compare and Compare and contrast singly linked list with an array.?
Ans:-
- Arrays are linear data structures. Linked lists are linear and non-linear data
structures.
- Linked lists are linear for accessing, and non-linear for storing in memory
- Array has homogenous values. And each element is independent of each other
positions. Each node in the linked list is connected with its previous node which is a
pointer to the node.
- Array elements can be modified easily by identifying the index value. It is a
complex process for modifying the node in a linked list.
- Array elements can not be added, deleted once it is declared. The nodes in the
linked list can be added and deleted from the list.