Search This Blog

Coding Class XI

//WAP to read marks of 3 students and store them under an array. #include<iostream.h>
#include<conio.h>
void main()
{
     const int size=3;
     float marks[size];
     //    cout<<"Enter marks of student"<<i+1<<"\n";
     for(int i=0; i<size;i++)
     {
           cout<<"Enter marks of student"<<i+1<<"\n";
           cin>>marks[i];
     }
     cout<<"\n";
     for(i=0;i<size;i++)
     cout<<"Marks["<<i<<"]="<<marks[i]<<"\n";
     getch();
}
-----------------------------------------------------------------
/*Program to read prices of 5 goods in an array and then display sum of all the prices, product of all the prices and average of them*/
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     const size=5;
     double price[5], avg, sum,prod;
     avg=sum=0;
     prod = 1;
     for(int i=0;i<size;++i)
     {
     cout<<"Enter price for "<<i+1<<" goods: "<<"\n";
     cin>>price[i];
     sum+=price[i];
     prod*=price[i];
     }
     avg=sum/5;
     cout<<"Sum of all prices "<<sum<<"\n";
     cout<<"Average of all prices "<<avg<<"\n";
     cout<<"Product of all prices "<<prod<<"\n";
     getch();

}


-----------------------------------------------------------------

/*
WAP to sort an array on N numbers in ascending order.
Avoid duplication of elements.
*/
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int A[20], N, i, j, temp;
     cout<<"\nEnter the number of elements:";
     cin>>N;
     for(i=0;i<N;i++)
     cin>>A[i];
     //Bubble sort technique
     for(i=0;i<N;++i)
           for(j=0;j<(N-1)-i; j++)
           if(A[j]>A[j+1])
                {
                     temp=A[j];
                     A[j]=A[j+1];
                     A[j+1]=temp;
                     }
     cout<<"The elements in the array after sorting...";
     for(i=0;i<N;i++)
     {
     cout<<A[i]<<'\t';
     }
     getch();
     }



-----------------------------------------------------------------

//WAP to add two matrices
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
     int a[10][10], b[10][10], c[10][10];
     int i, j, m, n, p, q;
     cout<<"\nInput row & column of matrix-A \n";
     cin>>m>>n;
     cout<<"\nInput row & column of matrix-B \n";
     cin>>p>>q;
     if((m==p)&&(n==q))
           cout<<"Matrix can be added\n";
     else
     {
           cout<<"Matrix cannot be added\n";
           exit(0);
     }
     cout<<"\nInput matrix-A\n";
     for(i=0;i<m;i++)
     {
           for(j=0;j<n;j++)
           cin>>a[i][j];
     }
     cout<<"\nMATRIX-A: ";
     for(i=0; i<m; i++)
     {
           cout<<"\n";
           for(j=0;j<n;j++)
                cout<<" "<<a[i][j];
     }
     cout<<"\nInput Matrix-B\n";
     for(i=0;i<p;i++)
     {
           for(j=0;j<q;j++)
                cin>>b[i][j];
     }
     cout<<"\nMATRIX-B: ";
     for(i=0;i<p;i++)
     {
           cout<<"\n";
           for(j=0;j<q;j++)
                cout<<" "<<b[i][j];
     }
     for(i=0;i<m;i++)
     {
           for(j=0;j<n;j++)
           c[i][j]=a[i][j] + b[i][j];
     }
     cout<<"\nThe sum of two matrix is :\n";
     for(i=0;i<m;i++)
     {
           cout<<"\n";
           for(j=0;j<n;j++)
           cout<<" "<<c[i][j];
     }
      //   return 0;
           getch();
}

-----------------------------------------------------------------

//WAP to add and subtract two matrices
#include<iostream.h>
#include<conio.h>
void main()
{
   int i,j,a[10][10],b[10][10],c[10][10],d[10][10],n,m;
   clrscr();
   cout<<"\nEnter the Number of Rows and Columns of Matrix A and B:";
   cin>>n>>m;
   cout<<"\nEnter the Elements of Matrix A: \n";
   for(i=0; i<n;i++)
    {
             for(j=0;j<m;j++)
            {
               cin>>a[i][j];
            }
    }
        cout<<"\nEnter the Elements of Matrix B: \n";
        for(i=0; i<n; i++)
           {
        for(j=0;j<m;j++ )
                   {
                        cin>>b[i][j];
                   }
          }
        for(i=0; i<n; i++)
    {
        for(j=0;j<m ;j++)
            {
                c[i][j]=a[i][j]+b[i][j];
                d[i][j]=a[i][j]-b[i][j];
            }
    }
        cout<<"\nThe Resultant Matrix C=A+B is :\n";
        for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++ )
            {
                cout<<c[i][j]<<" ";
            }
                cout<<"\n";
    }
                cout<<"\n The Resultant Matrix D=A-B is : \n";
        for(i=0; i<n; i++)
    {
        for (j=0; j<m; j++ )
                {
                     cout<<d[i][j]<<" ";
                }
                cout<<"\n";
    }

        getch();
}

-------------------------------------------------------------------------

//WAP to multiply any two 3x3 Matrices.
#include<iostream.h>
#include<conio.h>
void main( )
{
           int mat1 [3][3], mat2[3][3],mat3[3][3], i ,j, k, sum;
           clrscr( ) ;
           cout<<"\nEnter values for first 3 x 3 matrix:\n";
           for ( i = 0 ; i <= 2 ; i++ )
                {
                   for (j = 0 ; j <= 2 ; j++ )
                        cin>>mat1 [i][j] ;
                }
                        cout<<"\n Enter values for second 3 x 3 matrix:\n";
                   for ( i = 0 ; i <= 2 ; i++ )
                        {
                            for ( j = 0 ; j <= 2 ; j++ )
                                   cin>>mat2[i][j] ;
                        }
                       cout<<"\n The first 3 x 3 matrix entered by you is:\n";
                for ( i = 0 ; i <= 2 ; i++ )
                    {
                for ( j = 0 ; j <= 2 ; j++ )
                       cout<<"\t"<< mat1[i][j] ;
                       cout<<"\n";
                    }
                       cout<<"\n the second 3 x 3 matrix entered :\n";
                for ( i = 0 ; i <= 2 ; i++ )
                    {
                for ( j = 0 ; j <= 2 ; j++ )
                       cout<<"\t"<< mat2[i][j] ;
                       cout<<"\n";
                    }
                for ( i = 0 ; i <= 2 ; i++ )
                {
                for ( j = 0 ; j <= 2 ; j++ )
                    {
                        sum = 0;
                     for ( k = 0 ; k <=2 ; k++ )
                       sum = sum + mat1 [i][k] * mat2[k][j];
                       mat3[i][j] = sum ;
                    }
                     }
                cout<<"\nThe product of the above two matrices is:\n";
                for ( i = 0 ;i<= 2 ; i++ )
                     {
                for ( j = 0 ; j <= 2 ; j++ )
                                cout<<"\t"<<mat3[i][j] ;
                        cout<<"\n";
                }
                        cout<<"\n Press any key to exit.";
                        getch( ) ;
}

-------------------------------------------------------------------------

//WAP to print largest element of an array using a function.
#include<iostream.h>
#include<conio.h>
int Large(int[],int);
void main()
{
     clrscr();
     int A[50],i,n,MAX;
     cout<<"\nEnter the size of the array: ";
     cin>>n;
     cout<<"\nEnter the elements of the array: \n";
     for(i=0;i<n;i++)
           cin>>A[i];
     MAX=Large(A,n);
     cout<<"\nLargest element of the given array is: "<<MAX;
     getch();
}
int Large(int B[],int n)
{
           int max,i;
           max=B[0];
          for(i=1;i<n;i++)
           {
                if(max<B[i])
                     max=B[i];
           }
           return max;
}

-------------------------------------------------------------------------
//WAP to read an alphabet and display its next character in the ASCII list.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
     clrscr();
     char ch, Next;
     cout<<"\nEnter any character :";
     ch=getchar();
     Next=ch+1;
     cout<<"\nThe next character is:";
     putchar(Next);
     getch();
}

-------------------------------------------------------------------------

//WAP to count number of spaces in the string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
     clrscr();
     int i=0,count=0;
     char str[80];
     cout<<"Enter a string: ";
     gets(str);
     for(i=0;str[i]!='\0';i++)
     if(str[i]==' ')
     count++;
     cout<<"No. of spaces in the string are "<<count;
     getch();
}

-------------------------------------------------------------------------

//WAP to count number of lowercase characters entered.
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int chcount = 0;
     const char ent = '\n';
     char ch;
     cout<<"Enter character\n";
     cin.get(ch);
     while(ch!=ent)
     {
           if(ch>='a'&&ch<='z')
           {
                chcount++;
                cout.put(ch);
           }
           cin.get(ch);
     }
     cout<<"\nThe number of characters = "<<chcount<<"\n";
     getch();
}

-------------------------------------------------------------------------

//WAP to compare lengths of two strings.
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
     clrscr();
     char string1[50], string2[50];
     cout<<"Enter strings \n";
     cin.getline(string1,50);
     cin.getline(string2,50);
     int i,j;
     for(i=0;string1[i]!='\0';i++);
     for(j=0;string2[j]!='\0';j++);
     if(i==j)
           cout<<"\nBoth strings contain equal number of characters.";
     else
           cout<<"\nThe given strings have different number of characters.";
           getch();
}

-------------------------------------------------------------------------

//WAP to print cube of a given number using a function.
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     float cube(float);
     float x,y;
     cout<<"\nEnter number whose cube is to be calculated:\n";
     cin>>x;
     y=cube(x);
     cout<<"\nThe cube of "<<x<<" is "<<y<<"\n";
     getch();
}
float cube(float a)
{
     float n;
     n=a*a*a;
     return (n);
}

-----------------------------------------------------------------


//WAP to illustrate the call by value method of function invoking.
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int change(int);
     int orig = 10;
     cout<<"\nThe original value is "<<orig<<"\n";
     cout<<"\nReturn value of function change() is "<<change(orig)<<"\n";
     cout<<"\nThe value after function change() is over "<<orig<<"\n";
     getch();
}
int change(int a)
{
     a=20;
     return a;
}

-----------------------------------------------------------------

//WAP to swap two values.
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     void swap(int &,int &);
     int a,b;
     a = 7;
     b = 4;
     cout<<"\nThe original values are: \n";
     cout<<"a = "<<a<<",b = "<<b<<"\n";
     swap(a,b);
     cout<<"\nThe values after swap() are: \n";
     cout<<"a = "<<a<<",b = "<<b<<"\n";
     getch();
}
void swap(int &x, int&y)
{
     int temp;
     temp = x;
     x = y;
     y = temp;
     cout<<"\nThe swapped values are: \n";
     cout<<"a = "<<x<<",b = "<<y<<"\n";
}

------------------------------------------------------------------------

//WAP to convert distance in feet or inches using a call by reference method.
#include<iostream.h>
#include<process.h>
#include<conio.h>
void main()
{
     clrscr();
     void convert(float &, char &, char);
     float distance;
     char choice, type='F';
     cout<<"\nEnter distance in feet: ";
     cin>>distance;
     cout<<"\nYou want distance in feets or inches? (F/I): \n";
     cin>>choice;
     switch(choice)
     {
           case 'F': convert(distance,type,'F');
                break;
           case 'I': convert(distance,type,'I');
                break;
           default: cout<<"\nYou have entered a wrong choice!!! ";
                exit(0);
     }
     cout<<"\nDistance = "<<distance<<" "<<type<<"\n";
     getch();
}
void convert(float &d, char&t,char ch)
{ switch(ch)
     {    case 'F':if(t=='I')
           {
                d=d/12;
                t='F';
           }
           break;
           case 'I':if(t=='F')
           {
                d=d*12;
                t='I';
           }
           break;
     }
     getch();
}

-----------------------------------------------------------------

/*WAP to check whether a given character is contained in
a string or not and find its position*/
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
int findpos(char s[],char c);
     char string[80], ch;
     int y = 0;
     cout<<"Enter main string: \n";
     cin.getline(string,80);
     cout<<"\nEnter character to be searched for: \n";
     cin.get(ch);
     y = findpos(string,ch);
     if(y==-1)
           cout<<"\nSorry! character is not in the string\n";
     getch();
}
int findpos(char s[], char c)
{
     int flag = -1;
     for(int i = 0;s[i]!='\0';i++)
     {if(s[i]==c)
           {    flag = 0;
                cout<<"\nThe character is in the string at position "<<i+1;
           }
     }
     return(flag);
}

-----------------------------------------------------------------

//WAP to check if a string is palindrome or not.
#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     char string[80],c;
     cout<<"Enter string(max. 79 characters): ";
     cin.getline(string,80);
     //Loop to find the length of the string.
     for(int len=0;string[len]!='\0';len++);
     int i,j,flag=1;
     for(i=0,j=len-1;i<len/2;i++,j--)
     {
           if(string[i]!=string[j])
           {
                flag = 0;
                break;
           }
     }
     if(flag!=0)
           cout<<"It is a palindrome.\n";
     else
           cout<<"It is not a palindrome.\n";
     getch();
}

-------------------------------------------------------------------------

//WAP to replace every space in a string with a hyphen.
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
     clrscr();
     char string[80];
     cout<<"Enter string(max. 79 characters)\n";
     cin.getline(string,80);
     int x1 = strlen(string);
     for(int i=0;string[i]!='\0';i++)
           if(string[i]==' ')
                string[i] = '-';
     cout<<"The changed string is \n";
     cout.write(string, x1);
     getch();
}

-------------------------------------------------------------------------

//WAP to convert a string to proper case
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
     clrscr();
     char str[80];
     int i;
     cout<<"\nEnter any string(max. 80 characters): ";
     gets(str);
     str[0] = toupper(str[0]);
     for(i=0;str[i]!='\0';i++)
           if(str[i]==' ')
                str[i+1]=toupper(str[i+1]);
     cout<<"\nUpdated string is: "<<str;
     getch();
}

-------------------------------------------------------------------------

//WAP to find number of vowels in a given line of text.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
     clrscr();
     char line[80];
     int vow_ctr=0;
     cout<<"Enter the line:"<<endl;
     gets(line);
     for(int i=0; line[i]!='\0';++i)
     {
           switch(line[i])
           {    case 'a':
                case 'A':
                case 'e':
                case 'E':
                case 'i':
                case 'I':
                case 'o':
                case 'O':
                case 'u':
                case 'U': ++vow_ctr;
           }
     }
     cout<<"The total number of vowels in the given line is "<<vow_ctr<<endl;
     getch();
}

------------------------------------------------------------------------------------------------------------

//WAP to accept and print a student's result using a structure having array inside it.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
struct Student
{
     int rollno;
     char name[21];
     float marks[5];
     char grade;
};
Student learner;
void main()
{
     clrscr();
     cout<<"\n"<<"Enter Roll no.: ";
     cin>>learner.rollno;
     cout<<"\n"<<"Enter Name: ";
     gets(learner.name);
     cout<<"\n"<<"Enter marks in 5 subjects: "<<"\n";
     for(int i=0;i<5;i++)
     {
           cout<<"\n"<<"Subjects"<<i+1<<": ";
           cin>>learner.marks[i];
     }
     float avg, total;
     total = (learner.marks[0]+learner.marks[1]+learner.marks[2]
     +learner.marks[3]+learner.marks[4]);
     avg = total/5;
     if(avg<50)learner.grade = 'F';
     else if(avg<60) learner.grade = 'C';
     else if(avg<80) learner.grade = 'B';
     else learner.grade = 'A';
     cout<<"\n"<<"\n"<<"Student Result: "<<"\n";
     cout<<"Roll no: "<<learner.rollno<<"\t";
     cout<<"Name: ";
     cout.write(learner.name,21);
     cout<<"\n"<<"Total Marks: "<<total;
     cout<<"\t"<<"Grade: "<<learner.grade<<endl;
     getch();
}

-----------------------------------------------------------------

//WAP to illustrate passing of structures by value.
#include<iostream.h>
#include<conio.h>
struct Distance
{
     int feet;
     int inches;
};
void main()
{
     Distance length1, length2;
     void prnsum (Distance l1, Distance l2);
     cout<<"Enter length 1: "<<"\n";
     cout<<"Feet: ";
     cin>>length1.feet;
     cout<<"\n"<<"Inches: ";
     cin>>length1.inches;
     cout<<"\n\nEnter length2: "<<"\n";
     cout<<"Feet: ";
     cin>>length2.feet;
     cout<<"\n"<<"Inches: ";
     cin>>length2.inches;
     prnsum(length1,length2);
     getch();
}
void prnsum(Distance l1, Distance l2)
{
     Distance l3;
     l3.feet = l1.feet + l2.feet + (l1.inches + l2.inches)/12;
     l3.inches = (l1.inches + l2.inches)%12;
     cout<<"\n\nTotal Feet: "<<l3.feet<<"\n";
     cout<<"Total Inches: "<<l3.inches;
     getch();
}

-----------------------------------------------------------------


No comments:

Post a Comment

How do I crack the GSOC in the field of machine learning?

How do I crack the GSOC in the field of machine learning? Working as a student at Google Summer of Code demands for your dedication an...