0% found this document useful (0 votes)
2 views10 pages

Program -ML&CP Lab

The document contains several practical programming exercises in C, including the 3N+1 problem, Minesweeper board setup, average calculation for student scores, stack and queue implementations, and directory listing. Each section provides code examples, input formats, and expected output. The exercises cover basic data structures and algorithms, demonstrating fundamental programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Program -ML&CP Lab

The document contains several practical programming exercises in C, including the 3N+1 problem, Minesweeper board setup, average calculation for student scores, stack and queue implementations, and directory listing. Each section provides code examples, input formats, and expected output. The exercises cover basic data structures and algorithms, demonstrating fundamental programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

// Practical No: 01 *3N+1 PROBLEM* //

#include<stdio.h>
int find_max_cycle(int,int);
void main()
{
int start, end,maxcycle;
clrscr();
for(; ;)
{
scanf("%d %d",&start, &end);
if(start==1 || start>end)
exit(0);
maxcycle=find_max_cycle(start,end);
printf("%d %d %d \n",start,end,maxcycle);
}
}
int find_max_cycle(int s,int e)
{
int i,max=-1,n,length;
for(i=s; i<=e; i++)
{
n=i,length=0;
while(n!=1)
{
if(n%2==0)
{
n=n/2;
else
n=(3*n)+1;
}
length++;
}
}
if(max<length)
max=length;
return (max);
}
Input Format

Input consists of a number n(1<=n<=1000000).

Output Format

Print cycle length of n.

Sample Input

22

Sample Output

16
// Practical No:02 *MINESWEEPER BOARD* //

#include<stdio.h>
#include<conio.h>
int board[9][9];
void main()
{
int i,j,br,bc;
clrscr();
for(i=0;i<9;i++)
for(j=0;j<9;j++)
board[i][j]=0;
printf("\n Enter the position of 8 bombs");
for(i=0;i<8;i++)
{
scanf("%d %d",&br,&bc);
board[br][bc]=-1;
}
for(j=0;j<9;j++)
{
if(board[i][j]==-1)
{
if(safe(i,j-1)==1)
board[i][j-1]+=1;

if(safe(i-1,j-1)==1)
board[i-1][j-1]+=1;

if(safe(i-1,j)==1)
board[i-1][j]+=1;

if(safe(i-1,j+1)==1)
board[i-1][j+1]+=1;

if(safe(i,j+1)==1)
board[i][j+1]+=1;

if(safe(i+1,j+1)==1)
board[i+1][j+1]+=1;

if(safe(i+1,j)==1)
board[i+1][j]+=1;

if(safe(i+1,j-1)==1)
board[i+1][j-1]+=1;
}
}
for(i=0;i<9;i++)
{
printf("\n");

for(j=0;j<9;j++)
{
printf("\t %d",board[i][j]);
}
}
getch();
}

int safe(int pr,int pc)


{
int retval=0;
if(pr>=0 && pr<=8 && pc>=0 && pc<=8)
{
if(board[pr][pc]= -1)
retval=1;
}
return retval;
}

Input:

9 //Number of Rows

9 //Number of Columns

8 //Number of Mines

11

22

33

78

65

56

33

24

Output: The board cell values displayed in rows and columns, where cell with mine
represented by @ and programmed as -1.
// Practical No:03 * THE TRIP PROBLEM * //

#include<stdio.h>
#include<conio.h>
void main()
{
int n=0,i;
float sum=0.0,avg=0.0,a[100],exchange=0.0;
clrscr();
printf("\n enter the number of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
sum = avg+a[i];
}
avg = sum + a[i];
printf("\n avg: %f",avg);

for(i=0;i<n;i++)
{
if(a[i]<avg)
exchange = exchange + (avg-a[i]);
}
printf("\n %f",exchange);
getch();
}

Sample Input

3
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
0
Sample Output
$10.00
$11.99
// Practical No:04 STACK
#include<stdio.h>
#include<conio.h>

#define SIZE 10

void push(int);
void pop();
void display();

int stack[SIZE], top = -1;

void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value){
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop(){
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}
// Practical No:05 QUEUE
#include<stdio.h>
#define max 5
int queue[max];
int front=-1;
int rear=-1;
void insert();
void delete();
void display();
void main()
{
int ch;
clescr();
for(;;)
{
printf("\n QUEUE MENU");
printf("\n 1.INSERT");
printf("\n 2.DELETE");
printf("\n 3.DISPLAY");
printf("\n 4.EXIT");
printf("\n ENTER YOUR CHOICE");
scanf("%d",ch);
switch(ch)
{
case 1: insert();
break;

case 2: delete();
break;

case 3: display();
break;

case 4: exit(0);

default: printf("\n Invalid choice");

}
}

void insert()
{
if(rear==(max-1))
printf("\n queue is full");
else
{
rear++;
printf("\n Enter Element");
scanf("%d",&queue[rear]);
}
}
void delete()
{
int i;
if(rear==-1)
printf("\n queue is empty");
else
{
for(i=0;i<rear;i++)
queue[i]=queue[i+1];
rear--;
}
}

void display()
{
int i;
if(rear==-1)
printf("\n queue is empty");
else

{
for(i=0;i<=rear;i++)
printf("\t %d",queue[i]);
}
}
// Practical No: 06 Listing the contents of directory
#include<stdio.h>
#include<dirent.h>
int main(int argc,char *avgv[])
{
DIR *dp;
struct dirent *dirp;
if(argc!= 2)
{
printf("\n use the program name.exe dir.path");
exit(0);
}

if(dp = opendir(argv[1])==NULL)
{
printf("\n %s doesn't exists",arg[1]);
exit(0);
}

while(dirp=readdir(dp)!=NULL)

{
printf("\n %s",dirp->d_name);
}

closedir(dp);
return(0);
}

You might also like