Brain Tester : For Programmers


you entered a bus

-in that you saw 7 girls

-each girl has 7 bags

-in each bag there are 7 big cats.

-Each big cat has 7 little cats

-each cat has 4 legs

calculate the legs in the bus?

Let's see who gives correct

answer with proper explaintion.

C program to sort the array elements in ascending order using Selection Sort


#include<stdio.h>
#include<conio.h>

#define SIZE 20

void selection_sort(int arr[]);

void main()
{
    int i,j;
    int array_to_sort[SIZE];
    clrscr();

    //Get the elements of Array
    for(i=0; i<SIZE; i++)
    {
        j = i+1;
        printf("\nEnter element %d: ",j);
        scanf("%d", &array_to_sort[i]);
    }
   
    //Print the unsorted array.
    printf("\n\nUnsorted elements of Array are: ");   
    for(i=0; i<SIZE; i++)
    {
        printf("%d ",array_to_sort[i]);
    }
   
    //Sort array.
    selection_sort(array_to_sort);
   
    //Print the sorted array.
    printf("\n\nSorted elements of Array are: ");   
    for(i=0; i<SIZE; i++)
    {
        printf("%d ",array_to_sort[i]);
    }

    getch();
}

//Logic for Selection Sort.
void selection_sort(int arr[])
{
    int i, j, min_value, min_location, temp;
   
    for(i=0; i<SIZE-1; i++)
    {
        min_value = arr[i];
        min_location = i;
       
        for(j=i+1; j<SIZE; j++)
        {
            if(min_value > arr[j])
            {
                min_value = arr[j];
                min_location = j;
            }
        }
       
        if(min_location != i)
        {
            //Swap arr[i] with arr[min_location]
            temp = arr[i];
            arr[i] = arr[min_location];
            arr[min_location] = temp;
        }
    }
}

Some facts about C++

1.C++ was derived from C in the 80s

2.C++ is object oriented programming language

3.C++ supports structured programming as well

4.C++ is highly used because it's fast and middle level language and many other reasons

5.C++ supports multiple inheritance , Java does NOT.

6.C++ affected Java and C#

7.It's statically typed

8.It's strongly typed ?( u have to declare the variables )

9.It supports pointers ( access to memory addresses)

10.It supports preprocessing before compilation.

WAP of stack using structure and that needs to perform push & pop operation

/*WAP of stack using structure and that needs to perform push & pop operation*/
#include<iostream.h>
#include<conio.h>
//using namespace std;
void stack();
void push(int);
int pop();
struct stack
{
//private:
int arr[5],top;
//public:
}s;
void stack()
{
s.top=-1;
}
void push(int v)
{
if(s.top==4)
cout<<"Stack is full"<<endl;
else
{
s.arr[++s.top]=v;
cout<<"Data pushed successfully"<<endl;
}
}
int pop()
{
if(s.top==-1)
{
cout<<"Stack is empty"<<endl;
return NULL;
}
else
return s.arr[s.top--];
}
main()
{
///stack s;
int ch,k;
stack();
cout<<"1 for push\n2for pop";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Variable";
cin>>k;
push(k);
break;
case 2:
cout<<pop();
break;
}
getch();
}

WAP of stack that perform push,pop and peep operation using functions

/*stack-push,pop and peep*/
#include<stdio.h>
#include<conio.h>
int top=-1;
const int size=10;
int a[10];
void push();
void pop();
void peep();
void display();
void main()
{
int n;
char ch='y';
clrscr();
while(ch=='y')
{
printf("\n1.push\n2.pop\n3.peep\n4.display\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peep();
break;
}
case 4:
{
display();
break;
}
default:
{
printf("Invalid choice....");
break;
}
}
printf("\nDo you want to continue?");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void push()
{
int item;
if(top>=size)
{
printf("\nStack overflow.....\n");
}
else
{
top+=1;
printf("Enter element:");
scanf("%d",&item);
a[top]=item;

}
}
void pop()
{
int item;
if(top<0)
{
printf("\nStack underflow....\n");
}
else
{
item=a[top];
top-=1;
printf("\nValue deleted is:%d",item);
}

}
void display()
{
int i;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
for(i=0;i<=top;i++)
{
printf("\n%d",a[i]);
}
}
}
void peep()
{
int item;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
item=a[top];
printf("the top most element is %d",item);
}
}