Recent

Comments

Follow us on Facebook

Monday, 1 January 2018

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix.

- No comments
Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix.


#include<iostream>
using namespace std;
int main()
{
int arr[4][4],i,j;
cout<<"Enter elements of array: "<<endl;
                                           for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
cin>>arr[i][j];
}
}
cout<<"Daignal elements are :"<<endl;
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(i==j)
cout<<arr[i][j]<<"\t";
}
}
                             }

Take three values from the user and find discriminant then find the root if disc is greater than less than or equal to zero

- No comments
Take three values from the user and find discriminant then find the root if disc is greater than less
                                                       than or equal to zero

      #include <iostream>
#include <cmath>
using namespace std;

int main() {

    float a,b,c,R1,R2,disc,realPart,imgPart;
    cout<<"Enter value for a:";
    cin>>a;
    cout<<"Enter value for b:";
    cin>>b;
    cout<<"Enter value for c:";
    cin>>c;
 
 
    disc=b*b-4*a*c;
 
    if(disc>0)
{
        R1=(-b+sqrt(disc))/(2*a);
        R2=(-b-sqrt(disc))/(2*a);
        cout<<"Roots are real and different because discriminant is greater then zero '0'."<<endl;
        cout<<"R1 ="<<R1<<endl;
        cout<<"R2 ="<<R2<<endl;
    }
 
    else if(disc==0)
{
        cout<<"Roots are real and same because disciminant is equal to zero '0'."<<endl;
        R1=(-b/(2*a));
        cout<<"R1 = R2 ="<<R1<<endl;
    }

    else
{
        realPart=-b/(2*a);
        imgPart=sqrt(-disc)/(2*a);
        cout<<"Roots are complex and different because discriminant is less then zero '0'."<<endl;
        cout<<"R1 ="<<realPart<<"+"<<imgPart<<"i"<<endl;
        cout<<"R2 ="<<realPart<<"-"<<imgPart<<"i"<<endl;
    }

    return 0;
}

Write a program, that takes 12-Elements in Two-Dimensional-Array that has 3 rows & 4 columns, then Transpose of that Matrix & Display it.

- No comments
Write a program, that takes 12-Elements in Two-Dimensional-Array that has 3 rows & 4                             columns, then Transpose of that Matrix & Display it.

       #include <iostream>
using namespace std;

int main()
{
    int a[3][4],trans[4][3],i,j;



    cout<<endl<<"Enter elements of matrix: "<<endl;
    for(i=0; i<3; i++)
    {
    for(j=0; j<4; j++)
    {

        cin>>a[i][j];
    }
}

    cout<<endl<<"Entered Matrix: "<<endl;
    for(i=0; i<3; i++)
    {
        for(j=0; j<4; j++)
        {
            cout<<a[i][j]<<"\t";
        }
        cout<<endl;
    }
 
cout << endl << "Transpose of Matrix: " << endl;
    for(i=0; i<4; i++)
    {
        for(j=0; j<3; j++)
        {
            cout<<a[j][i]<<"\t";
        }
        cout<<endl;
}


}

Declare and define an array and sort the values in ascending order.

- No comments

Declare and define an array and sort the values in ascending order.

#include<iostream>
using namespace std;
main()
 {
 int i,a[10],temp,j;
 
 cout<<"Enter any 10 num in array: \n";
 for(i=0;i<=10;i++)
 {
 cin>>a[i];
 }
 cout<<"\nData before sorting: ";
 for(j=0;j<10;j++)
 {
 cout<<a[j];
 }
 for(i=0;i<=10;i++)
 {
 for(j=0;j<=10-i;j++)
 {
 if(a[j]>a[j+1])
 {
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
 }
 }
 }
 cout<<"\nData after sorting: ";
 for(j=0;j<10;j++)
 {
 cout<<a[j];
 }
 
 }

take a single dimensional array set the default values to 0 or -1, take choice from the user if a inser values the values for the array, if b update the values and if c delete values from the array.

- No comments
take a single dimensional array set the default values to 0 or -1, take choice from the user if a inser values  the values for the array, if b update the values and if c delete values from the array.
#include <iostream>
using namespace std;

int main()
{
int arr[5]={0},i,num;
char ss;
ii:

cout<<endl<<"OPTIONS: "<<endl;
cout<<endl<<"Press 'a' for inserting value in specific index."<<endl;
cout<<"Press 'b' for updating value in specific index."<<endl;
cout<<"Press 'c' for deleting value the specific index value."<<endl;

cout<<endl<<"Select option:";
cin>>ss;

cout<<endl;
switch(ss)
{
case 'a':
{

cout<<"enter index on which the number is to be add:";
cin>>i;
if(arr[i]==0)
{

cout<<"enter value to add: ";
cin>>num;
arr[i]=num;}

else
{
cout<<"selected index is not zero any more."<<endl<<endl;
}
}
break;

case 'b':
{

cout<<"enter index number which you want to update:";
cin>>i;
cout<<"enter updated value: ";
cin>>arr[i];
}
break;

case 'c':
{

cout<<"enter index number which you want to delet:";
cin>>i;
cout<<"enter 0 to delet value: ";
cin>>arr[i];

}
break;

default:
{
cout<<"Selected option is not correct...."<<endl;
cout<<"Choose the correct option from below menu:";
goto ii;
}
}
cout<<endl<<endl;
cout<<"Array elements are:";
for(int i=0; i<5; i++)
{
cout<<" "<<arr[i]<<"\t";
}
goto ii;
}

Write a program to pass array as an argument to the function.

- No comments
Write a program to pass array as an argument to the function.
#include<iostream>
using namespace std;
void display (int marks [5]);
int main()
{
int marks[5]={88,34,54,45,32};
display (marks);
return 0;
}
void display (int m[5])
{
cout<<"displaying marks "<<endl;
for (int i=0;i<5;++i)
{
cout<<"student"<<i+1<<"  "<<m[i]<<endl;
}
}

Write a program using array and swap first and last value of array.

- No comments

Write a program using array and swap first and last value of                                                       array.

# include<iostream>
using namespace std;
main()
{
int a[100],i,n,temp;
cout<<"Enter element of Array you want to insert "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the element "<<i+1<<endl;
cin>>a[i];
}
temp=a[0];
a[0]=a[n-1];
a[n-1]=temp;

cout<<"After the swaming the Array Are "<<endl;
for(i=0;i<5;i++)

cout<<a[i]<<endl;
return 0;
}


Implement stack

- No comments
 Implement stack
#include<stdio.h>

#define MAX_SIZE 101

int A[MAX_SIZE]; // integer array to store the stack 
int top = -1;  // variable to mark top of stack in array

// Push operation to insert an element on top of stack. 
void Push(int x) 
{
  if(top == MAX_SIZE -1) { // overflow case. 
printf("Error: stack overflow\n");
return;
}
A[++top] = x;
}

// Pop operation to remove an element from top of stack.
void Pop() 
{
if(top == -1) { // If stack is empty, pop should throw error. 
printf("Error: No element to pop\n");
return;
}
top--;
}

// Top operation to return element at top of stack. 
int Top() 
{
return A[top];
}

// This function will return 1 (true) if stack is empty, 0 (false) otherwise
int IsEmpty()
{
    if(top == -1) return 1;
    return 0;
}

// This function is just to test the implementation of stack. 
// This will print all the elements in the stack at any stage. 
void Print() {
int i;
printf("Stack: ");
for(i = 0;i<=top;i++)
printf("%d ",A[i]);
printf("\n");
}

int main() {
  // Code to test the implementation. 
  // calling Print() after each push or pop to see the state of stack. 
Push(2);Print();
Push(5);Print();
Push(10);Print();
Pop();Print();
Push(12);Print();
}

Write a program to pass an array of structure to the function.

- No comments
Write a program to pass an array of structure to the function.
#include<iostream>
using namespace std;

struct student
{
char fname[50];
char sname[50];
int id;
unsigned int  phone;
};
void fun(student[]);
main()
{
student s[3];
for(int i=0; i<3; i++)
{
cout<<"enter the data of student"<<i+1<<endl;
cout<<"enter the 1st name of the student"<<endl;
cin>>s[i].fname;
cout<<"enter the 2nd name of the student"<<endl;
cin>>s[i].sname;
cout<<"enter the id of the student :"<<endl;
cin>>s[i].id;
cout<<"enter the phone number of the student:"<<endl;
cin>>s[i].phone;
}
fun(s);
 }
 void fun(student r[])
 {

 system("CLS");
cout<<"      name    \t      id       \t     phone  "<<endl;
for(int i=0; i<3; i++)
{
cout<<i+1<<"  "<<r[i].fname<<" "<<r[i].sname<<"  \t    "<<r[i].id<<"     \t    "<<r[i].phone<<endl;
}
}

Write a program to enter data into a structure variable of array type that has 10 elements and each record contains three fields.

- No comments
Write a program to enter data into a structure variable of array type that has 10 elements and                                                     each record contains three fields.

#include<iostream>
using namespace std;

struct student
{
char fname[50];
char sname[50];
int id;
unsigned int  phone;

};
main()
{
student s[3];
for(int i=0; i<3; i++)
{
cout<<"enter the data of student"<<i+1<<endl;
cout<<"enter the 1st name of the student"<<endl;
cin>>s[i].fname;
cout<<"enter the 2nd name of the student"<<endl;
cin>>s[i].sname;
cout<<"enter the id of the student :"<<endl;
cin>>s[i].id;
cout<<"enter the phone number of the student:"<<endl;
cin>>s[i].phone;

 }
 system("CLS");
cout<<"      name    \t      id       \t     phone  "<<endl;
for(int i=0; i<3; i++)
{
cout<<i+1<<"  "<<s[i].fname<<" "<<s[i].sname<<"  \t    "<<s[i].id<<"     \t    "<<s[i].phone<<endl;
}


}




Sunday, 31 December 2017

Define a structure student , first function takes the values and pass it to the second function as an argument to display them

- No comments

Image result for c++


#include <iostream>
using namespace std;
struct student
{


char name [100];
int rollno;
};
student value(student);
void dispalydata(student);
main()
{
student v;
   v=value(v);
   dispalydata(v);
}
student value(student s)
{


cout<<"enter your Good name ";
cin.get(s.name,100);
cout<<"enter your roll no  ";
cin>>s.rollno;
return s;


}
void dispalydata(student s)
{
cout<<"\nfollowing is your information   ";
cout<<"\nNAME"<<"   "<<s.name<<endl;
cout<<"\nROLL NO"<<"   "<<s.rollno<<endl;

}

Define a structure student and take values of the members in main and pass the structure to the function as an agrument to display the values

- No comments

Image result for c++ structure

 pass the structure to the function as an agrument to display the values

#include <iostream>
using namespace std;
struct student
{


char name [30];
string  field;
string  semester;
int rollno;
};
void dispalydata(student);
main()
{
student s;

cout<<"enter your Good name ";
cin.get(s.name,100);
cout<<"which is your field  ";
    cin>>s.field;
cout<<"enter your roll no  ";
cin>>s.rollno;

cout<<"In which semeter you study  ";
cin>>s.semester;

dispalydata(s);
return 0;

}
void dispalydata(student s)
{
cout<<"\nfollowing is your information   ";
cout<<"\nNAME"<<"    "<<s.name<<endl;
cout<<"ROLL NO"<<"    "<<s.rollno<<endl;
cout<<"FEILD"<<"   "<<s.field<<endl;
cout<<"semester"<<"    "<<s.semester<<endl;

}

Sunday, 24 December 2017

1. Write a function that is called by function main () and receives five integers. The function should print the sum of five integers and return the average to main. The average is printed in main.

- No comments
#include <iostream>
using namespace std;
main()
{
int avg (int,int,int,int,int);
int a,b,c,d,e,z;
cout<<"\nenter the five number for sum and avg  ";
cin>>a>>b>>c>>d>>e;
z=avg(a,b,c,d,e);
cout<<"\nthe avg of five number is   =      "<<z;

}
int avg (int s,int t,int u,int v,int r)
{
int sum,avg;
sum=s+t+u+v+r;

avg=sum/5;
    cout<<"\nsum is     =         "<<sum;
return avg;
}

Tuesday, 19 December 2017

write a program to add,update,delete and quit the menu of an array using function in c++

- No comments
#include <iostream>
using namespace std;

int add(int a[]);
int update(int a[]);
int delet(int a[]);

int main()
{
int arr[5]={0},ss,a,b,c,d,QUIT_CHOICE=4;
cout<<"OPTIONS: "<<endl<<endl;
ii:
cout<<endl<<endl<<"press 1 for adding."<<endl;
cout<<"press 2 for updating value."<<endl;
cout<<"press 3 for deleting value."<<endl;
cout<<"press 4 for quit the program"<<endl;
cout<<"select option.";
cin>>ss;
if (ss != QUIT_CHOICE)
{
 
switch(ss)
{
case 1:
add(arr);
break;

case 2:
b=update(arr);
break;

case 3:
c=delet(arr);
break;



}

     
cout<<"array elements are:";
for(int i=0; i<5; i++)
{
cout<<arr[i]<<"\t";
}
goto ii;
while (ss != QUIT_CHOICE);
         return 0;

}}
int add(int a[5])
{
int i,num;
cout<<"enter index on which the number is to be add:";
cin>>i;
cout<<"enter value to add: ";
cin>>num;
a[i]=a[i]+num;

return a[5];
}

int update(int a[5])
{

int i;
cout<<"enter index number which you want to update:";
cin>>i;
cout<<endl<<"enter updated value: ";
cin>>a[i];

return a[5];
}

int delet(int a[5])
{

int i;
cout<<"enter index number which you want to delet:";
cin>>i;
cout<<endl<<"enter 0 to delet value: ";
cin>>a[i];

return a[5];
}














































Monday, 18 December 2017

how to sort integer array numbers/elements in Ascending Order in C++

- No comments
#include <iostream>
using namespace std;

#define MAX 100

int main()
{
 //array declaration
 int arr[MAX];
 int n,i,j;
 int temp;
 
 //read total number of elements to read
 cout<<"Enter total number of elements to read: ";
 cin>>n;
 
 //check bound
 if(n<0 || n>MAX)
 {
  cout<<"Input valid range!!!"<<endl;
  return -1;
 }
 
 //read n elements
 for(i=0;i<n;i++)
 {
  cout<<"Enter element ["<<i+1<<"] ";
  cin>>arr[i];
 }
 
 //print input elements
 cout<<"Unsorted Array elements:"<<endl;
 for(i=0;i<n;i++)
  cout<<arr[i]<<"\t";
 cout<<endl;
 
 //sorting - ASCENDING ORDER
 for(i=0;i<n;i++)
 {  
  for(j=i+1;j<n;j++)
  {
   if(arr[i]>arr[j])
   {
    temp  =arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
   }
  }
 }
 
 //print sorted array elements
 cout<<"Sorted (Ascending Order) Array elements:"<<endl;
 for(i=0;i<n;i++)
  cout<<arr[i]<<"\t";
 cout<<endl; 
 
 
 return 0;
 
}

Monday, 27 November 2017

4. Five different numbers are input in an array. Write a program to determine how many of them are positive, negative, even and odd

- No comments


Five different numbers are input in an array. Write a program to determine how many of them are positive, negative, even and odd

#include <iostream>
using namespace std;
main ()
{
int a[10],pos=0,neg=0,even=0,odd=0;
for(int i=0;i<10;i++)
{
cout<<"enter the number"<<i+1<<"   =  ";
cin>>a[i];
}
for (int i=0;i<10;i++)
{
if(a[i]>=0)
                  pos++;
            else
                  neg++;
            if(a[i]%2==0)
                  even++;
            else
                  odd++;
}
cout<<"positive number is = "<<pos<<endl;
cout<<"negative number is = "<<neg<<endl;
cout<<"even number is     = "<<even<<endl;
cout<<"odd number is      = "<<odd<<endl;
}

find sum and avg of number using array in c+= for loop

- No comments

find sum and avg of number using array in c+= for loop


#include <iostream>
using namespace std;
main ()
{
int n[10],sum,avg;
for(int i=0;i<10;i++)
{
cout<<"enter the number"<<i+1<<"   =  ";
cin>>n[i];
}
for (int i=0;i<10;i++)
{
sum+=n[i];
avg=sum/5;
}
cout<<"sum  of the number is   =  "<<sum<<endl;
cout<<"avg of the number is    = "<<avg;
}

Sunday, 26 November 2017

how to find maximum and minimum number using array

- No comments

how to find maximum and minimum number using array

#include<iostream>
using namespace std;
main(){

double v[5];

for (int i=0;i<5;i++)
{
cout<<"enter the value of interger  ";
cin>>v[i];
}
 double max = v[0];
 double min = v[0];
  for (int i=1;i<5;i++)
  {
  if (v[i]< min)
  {
  min=v[i];
  }
  if (v[i]>max)
  {
  max=v[i];
  }
 
  }
  cout<<"maximun number is "<<max<<endl;
  cout<<"minimum number is "<<min<<endl;
}

how to make snake game using c++

- No comments

Image result for snake in C plus plus

make snake game in c++

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 50;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirecton dir;
void Setup()
{
    gameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}
void Draw()
{
    system("cls"); //system("clear");
    for (int i = 0; i < width+2; i++)
        cout << "#";
    cout << endl;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (j == 0)
                cout << "#";
            if (i == y && j == x)
                cout << "O";
            else if (i == fruitY && j == fruitX)
                cout << "F";
            else
            {
                bool print = false;
                for (int k = 0; k < nTail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        cout << "o";
                        print = true;
                    }
                }
                if (!print)
                    cout << " ";
            }
               

            if (j == width - 1)
                cout << "#";
        }
        cout << endl;
    }

    for (int i = 0; i < width+2; i++)
        cout << "#";
    cout << endl;
    cout << "Score:" << score << endl;
}
void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        case 'x':
            gameOver = true;
            break;
        }
    }
}
void Logic()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < nTail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    switch (dir)
    {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }
    if (x > width || x < 0 || y > height || y < 0)
      gameOver = true;
    if (x >= width) x = 0; else if (x < 0) x = width - 1;
    if (y >= height) y = 0; else if (y < 0) y = height - 1;

    for (int i = 0; i < nTail; i++)
        if (tailX[i] == x && tailY[i] == y)
            gameOver = true;

    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        nTail++;
    }
}
int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(10); //sleep(10);
    }
    return 0;
}

Monday, 20 November 2017

Arithmetic Operations *********************************************************************************** 1) Press ‘ +‘ to add two numbers 2) Press ‘-‘ to subtract two numbers 3) Press ‘/’ to divide two numbers 4) Press ‘*’ to multiply two numbers *********************************************************************************

- No comments
QNo1.
***********************************************************************************
                                                                Arithmetic  Operations                                                                                
***********************************************************************************
1)      Press  ‘ +‘  to add two  numbers
2)      Press  ‘-‘  to subtract two numbers
3)      Press ‘/’  to divide two numbers
4)      Press ‘*’ to multiply two numbers

************************************************************************************
Enter ist  Number :
Enter 2nd Number:
Select arithmetic operator from menu:
Result of Addition/Subtraction/Multiplication/Division is:

i)                    Create Menu as given above
ii)                   Take input from user for two integers
iii)                 Using switch statement, perform operation on two numbers as specified by user

*Note:  User should be restricted to enter correct operator. If operator is not valid, prompt the user to enter correct operator. Abstraction/deletion/ Multiplication/Division can be performed only when user enters correct operator.



# include <iostream>
using namespace std;

int main()
{
    char op;
    float num1, num2;
    cout<<"************************************************************************************************************************ ";
    cout<<" \n                               arthematic operation                                                                        ";
    cout<<"\n************************************************************************************************************************ ";

    cout <<"                              \n1)Press + to add number                                                                      ";
    cout<<"                               \n2)press - to subtract number                                                                 ";
    cout<<"                               \n3)press * to  multiple number                                                                ";
    cout<<"                                \n 4)press / to divided number                                                                ";
    cout<<"\n************************************************************************************************************************";
    cin >> op;
    cout << "Enter firt operands       =      : ";
    cin >> num1 ;
    cout<<"Enter second operands       =      : ";
    cin>> num2;

    switch(op)
    {
        case '+':
            cout << num1+num2;
            break;

        case '-':
            cout << num1-num2;
            break;

        case '*':
            cout << num1*num2;
            break;

        case '/':
            cout << num1/num2;
            break;

        default:
            cout << "Error! operator is not correct";
            break;
    }

    return 0;

}

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix.

Write a program, that takes 16-Elements in Two-Dimensional-Array that has 4 rows & 4 columns, then display the diagonal of Matrix. ...

propeller ads

PropellerAds