Question no:1
Write a program that will define an integer array test[] of size 10. The program will read values into the array test[] through keyboard from the user. After reading into the array test[] the program will prompt the user to Enter a value to be searched:. In response, the user will enter a value through keyboard and the program will store the value in an integer variable value. The program will then pass the array test[]and the variable value to a function find(). The functionfind() will search the array test[] for the given element value. If the element was found then a message The element was found and the location is is displayed. If the element was not found then a message The element was not found. is displayed. The result is displayed in the find() function.
#include<iostream.h> #include<conio.h> #include<stdio.h> void find (int test[],int ele); void main() { int a[10],i,num; clrscr(); cout<<"enter the numbers"<<endl; for(i=1;i<=10;i++) cin>>a[i]; cout<<"enter the element to searched"<<endl; cin>>num; find(a,num); getch(); } void find(int test[],int ele)
{ int j; int temp=0,pos=0; for(j=1;j<=10;j++) { if(test[j]==ele) {temp=1; pos=j; } } if(temp==1) cout<<"element found and position of"<<ele<<"is"<<pos<<endl; else cout<<"element not found"<<endl; getch(); }
Question no:2
Write a program that will declare a class point. The class point has two private data members x and y of type float. The class point has a parameterized constructor to initialize both the data members i.e. x and y. Theclass point has a member function display() that display the value of x and y. Create two objects p1 and p2 with your desired data and display the values of x and y of p1 and p2. Now create a third object p3 by using the expression p3=p1+p2and display the values of x and y of p3.
#include<iostream.h>
#include<conio.h> #include<String.h> #include<stdio.h> class point { private: int x,y; public: point() { x=1; y=2; } point(int n,int c) { x=n; y=c; } void display() { cout<<"x="<<x<<endl; cout<<"y="<<y<<endl; } void add(point n) { point p;
p.x=x+n.x; p.y=y+n.y; cout<<"x="<<p.x<<endl; cout<<"y="<<p.y<<endl;} };
void main() { clrscr(); point p1,p2(3,3); point p3,your; cout<<"point 1 is"<<endl; p1.display(); cout<<"point 2 is"<<endl; p2.display(); cout<<"point 3 is"<<endl; p3.add(your); getch(); }
Question no :3
Write a program that takes the first 100 non-zero integers, that is the numbers 1-100 in RANDOM order, and stores these numbers into an array of size 100. (You may choose to hard code the numbers in the program or take input from the keyboard). The program then sorts the array in ascending order and displays the sorted array on the screen .
#include<iostream.h>
#include<conio.h> void main() { clrscr(); int arr[99],i,j,temp; { cout<<"Enter the digits"<<endl; for(i=0;i<99;i++) cin>>arr[i]; cout<<"The original digits are"<<endl;; for(i=0;i<99;i++) cout<<arr[i]<<endl; for(i=0;i<99;i++) for(j=0;j<98;j++) if (arr[j]<arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } cout<<"the Sorted array is"<<endl; for(i=0;i<99;i++) cout<<arr[i]<<endl; } }
Question no :4
Write a small C Program that inputs a sentence from the user and displays the entered sentence in reverse order.