Skip to content

Instantly share code, notes, and snippets.

@arnobk
Last active October 12, 2017 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnobk/310600b779d648a64867cd37c5311b0d to your computer and use it in GitHub Desktop.
Save arnobk/310600b779d648a64867cd37c5311b0d to your computer and use it in GitHub Desktop.
Semester Final Question Solve | EEE 2109 | SPRING 2017 | Programming Language
/*
LINE 1: Wrong header. Correct Header <iostream>.
LINE 2: Missing namespace declearation.
LINE 3: Missing Main Function Scope.
LINE 5: Wrong operator for cout. Correct one "<<".
Missing cout operator before endl.
LINE 6: Wrong operator for cin. Correct One ">>".
Variable a,b not decleared.
LINE 8: Semicolon after if statement.
LINE 10: Condition on else statement.
LINE 11: Wrong function to output information. We should use "cout".
*/
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;
cin>>n;
if(pow(int(sqrt(n)),2)==n)
cout<<n<<" is a square number"<<endl;
else cout<<n<<" is not a square number"<<endl;
}
#include<iostream>
using namespace std;
int main(){
int n1,n2,n3;
double w1,w2,w3;
double avg;
cout<<"Enter the three integers:"<<endl;
cin>>n1>>n2>>n3;
cout<<"Enter the weights of:"<<endl;
cout<<"First no. ";
cin>>w1;
cout<<"Second no. ";
cin>>w2;
cout<<"Third no. ";
cin>>w3;
avg = (n1*w1+n2*w2+n3*w3)/(w1+w2+w3);
cout<<"The weighted average of the numbers is: "<<avg<<endl;
}
#include<iostream>
using namespace std;
bool check(int x,int y,int z){
int min = x<y?(x<z?x:z):(y<z?y:z);
if(x%min==0 && y%min==0 && z%min==0)
return true;
else return false;
}
int main(){
int n1,n2,n3;
cout<<"Enter three numbers: "<<endl;
cin>>n1>>n2>>n3;
if(check(n1,n2,n3))
cout<<"The smallest number is a factor of other two."<<endl;
else cout<<"The smallest number is not a factor of other two."<<endl;
}
/* If we do not put break statement after the end of each
clause in a switch statement then it will execute all the
statements from the matching clause until the end of the
swtich statement.
For Example:
switch(n){
case 1: statement1;
case 2: statement2;
case 3: statement3;
case 4: statement3;
}
if n=2, then it will execute statement2, statement3 and statement 4.
*/
#include<iostream>
#include<complex>
#define PI 3.1416
using namespace std;
int main(){
double v, angle, c, r1, r2;
cout<<"Enter Voltage,Voltage Angle, Capacitance, Resistane1, Resistance2: "<<endl;
cin>>v>>angle>>c>>r1>>r2;
complex <double> voltage(v,angle*PI/180);
complex <double> xc(0,-1/(2*PI*50*c));
complex <double> res1 (r1,0);
complex <double> res2 (r2,0);
complex <double> zth = r2*xc/(r2+xc);
cout<<"Thevenin Resistance: "<<zth<<endl;
cout<<"Thevenin Voltage: "<<voltage*r1/(r1+zth)<<endl;
}
#include<iostream>
using namespace std;
void binary(int num){
int rem;
if (num <= 1){
cout << num;
return;
}
rem = num % 2;
binary(num / 2);
cout << rem;
}
int main(){
int dec, bin;
cout << "Enter the number : ";
cin >> dec;
cout << "The binary form of " << dec << " is ";
binary(dec);
cout << endl;
}
#include<iostream>
using namespace std;
int large(int x){
int max=0,n;
for(int i=0;i<x;i++){
cout<<"Enter Number "<<i+1<<": ";
cin>>n;
if(n>max) max = n;
}
return max;
}
int main(){
int a;
cout<<"How many numbers you want to check?"<<endl;
cin>>a;
cout<<"The largest number is "<<large(a)<<endl;
}
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter a number: ";
cin>>n;
while(n>1){
for(int i=2;i<=n;i++){
if(n%i==0){
cout<<i;
n /= i;
if(n>1)
cout<<"x";
break;
}
}
}
cout<<endl;
}
#include<iostream>
#include<complex>
using namespace std;
void quadratic(complex <double> &root1,complex <double> &root2){
float a,b,c;
cin>>a,b,c;
double test = b*b - 4*a*c;
if(test<0){
root1 = (-b/(2*a),sqrt(abs(test)/(2*a)));
root2 = (-b/(2*a),-sqrt(abs(test)/(2*a)));
} else{
root1 = ((-b+test)/(2*a),0);
root2 = ((-b-test)/(2*a),0);
}
}
int main(){
complex <double> root1(0,0);
complex <double> root2(0,0);
quadratic(root1,root2);
cout<<root1<<endl;
cout<<root2<<endl;
}
//some errors in it
#include<iostream>
#include<cmath>
using namespace std;
long long series(int n){
if(n==0) return 1;
return pow(7,n)*series(n-1);
}
int main(){
int a;
cout<<"Input n: ";
cin>>n;
cout<<series(n)<<endl;
}
#include<iostream>
using namespace std;
int main(){
int a = 5, b=14;
for(int i=0;i<5;i++){
for(int j=0;j<i;j++)
cout<<" ";
for(int k=i;k<5;k++)
if(i==k){
cout<<a--<<" ";
} else {
cout<<b--<<" ";
}
cout<<endl;
}
}
#include<iostream>
using namespace std;
int main(){
int p = 3,m=2,sum,fifty,hundred,max=0;
int a[p][m];
for(int i=0;i<p;i++){
cout<<"Batsman "<<i+1<<endl;
for(int j=0;j<m;j++){
cout<<"Match "<<j+1<<": ";
cin>>a[i][j];
}
}
for(int i=0;i<p;i++){
sum = 0;
fifty = 0;
hundred = 0;
for(int j=0;j<m;j++){
sum+=a[i][j];
if(a[i][j]>49 && a[i][j]<100)
fifty++;
if(a[i][j]>99)
hundred++;
if(a[i][j]>max) max = a[i][j];
}
cout<<"Avg of Batsman "<<i+1<<": "<<sum/double(m)<<endl;
cout<<"Half-century of Batsman "<<i+1<<": "<<fifty<<endl;
cout<<"Century of Batsman "<<i+1<<": "<<hundred<<endl;
}
for(int i=0;i<p;i++){
for(int j=0;j<m;j++){
if(a[i][j]==max){
cout<<"Highest runs scored by Batsman "<<i+1<<endl;
cout<<"Highest run was scored on Match "<<j+1<<endl;
break;
}
}
}
}
#include<iostream>
using namespace std;
int frequency(float a[],int n,int x){
int freq;
for(int i=0;i<n;i++){
if(a[i]==x){
freq++;
}
}
return freq;
}
int main(){
int n,x;
cout<<"How many numbers?"<<endl;
cin>>n;
float a[n];
cout<<"Enter the numbers:"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Find frequency of which number?"<<endl;
cin>>x;
cout<<"Frequency: "<<frequency(a,n,x)<<endl;
}
/*
x = 44
&x = 0x0028fefa
&rx = 0x0028fefa
rx = 43
px = 0x0028fefa
&px = 0x0028fef4
*px = 43
px = 0x0028fefe
*/
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char a[100];
cin.getline(a,100);
for(int i=strlen(a);i>=0;i--){
cout<<a[i];
}
cout<<endl;
}
/*
Object is born.
Object is born.
Object dies.
Object dies.
*/
#include<iostream>
using namespace std;
int gcd(int m,int n){
if(m<n)
swap(m,n);
while(m%n!=0){
int a = n;
n = m%n;
m=a;
}
return n;
}
int main(){
int m,n;
cout<<"Enter Two Numbers: ";
cin>>m>>n;
cout<<"GCD of these two number is: "<<gcd(m,n)<<endl;
}
#include<iostream>
using namespace std;
int main(){
int size,x;
int *ptr;
cout<<"Enter size of the array: ";
cin>>size;
int a[size];
cout<<"Enter Numbers: ";
for(int i=0;i<size;i++){
cin>>a[i];
}
ptr = a;
cout<<"Enter Number to search for address: ";
cin>>x;
for(int i=0;i<size;i++,ptr++){
if(*ptr == x ){
cout<<x<<" is found at address: "<<&ptr<<endl;;
return 0;
}
}
cout<<"No such integer in this array."<<endl;
}
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[] = "Good";
char s2[] = "bye";
strcat(s1,s2);
cout<<s1<<endl;
}
#include<iostream>
using namespace std;
int main(){
double speed[7];
double max=0;
cout<<"Enter speed of wind for 7 days: "<<endl;
for(int i = 0; i < 7; i++){
cin>>speed[i];
if(speed[i]>max) max = speed[i];
}
cout<<"Max wind speed: "<<max<<endl;
cout<<"Max speed were found on: ";
for(int i=0;i<7;i++){
if(speed[i]==max){
switch(i){
case 0: cout<<"Sunday ";break;
case 1: cout<<"Monday ";break;
case 2: cout<<"Tuesday ";break;
case 3: cout<<"Wednesday ";break;
case 4: cout<<"Thursday ";break;
case 5: cout<<"Friday ";break;
case 6: cout<<"Saturday ";break;
}
}
}
cout<<endl;
}
#include<iostream>
using namespace std;
int main(){
int size;
cout<<"Size of array(must be odd number): ";
cin>>size;
int a[size];
for(int i=0;i<size;i++){
cin>>a[i];
}
for(int i = 0; i<size;i++){
for(int j = 0;j<size-1;j++){
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
int mid = a[size/2];
cout<<"Middle Number is "<<mid<<endl;
if(mid%2==0) cout<<mid<<" is a even number."<<endl;
else cout<<mid<<" is a odd number"<<endl;
}
#include<iostream>
using namespace std;
class Minimum{
public:
void assign(int a,int b,int c){
n1 = a;
n2 = b;
n3 = c;
}
int findMinimum(){
return n1<n2?(n1<n3?n1:n3):(n2<n3?n2:n3);
}
private:
int n1,n2,n3;
};
int main(){
Minimum m;
int x,y,z;
cout<<"Enter Three Numbers: ";
cin>>x>>y>>z;
m.assign(x,y,z);
cout<<"Minimum number is "<<m.findMinimum()<<endl;
}
#include<iostream>
#define PI 3.1416
using namespace std;
class Rectangle{
public:
double height,width;
void getdata_Rect(){
cout<<"Enter Height of Rectangle: ";
cin>>height;
cout<<"Enter Width of Rectangle: ";
cin>>width;
}
void displayarea_Rect(){
cout<<"Area of Rectangle is "<<height*width<<endl;
}
};
class Circle{
public:
double radius;
void getdata_Circle(){
cout<<"Enter Radius of Circle: ";
cin>>radius;
}
void displayarea_Circle(){
cout<<"Area of Circle is "<<PI*radius*radius<<endl;
}
};
class Result:public Rectangle,public Circle{
public:
void getData(){
getdata_Rect();
getdata_Circle();
}
void display(){
displayarea_Rect();
displayarea_Circle();
}
};
int main(){
Result r;
r.getData();
r.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment