EECE 231 Lab 2 Solution, Fall 2014
American University of Beirut · Introduction to Programming Using C++ and MATLAB · 7 pages · Prof. Fadi Zaraket · with solution
More EECE 231 files
- Lab 1024 · Fall 2017LabView
- Lecture All C++ Slides · Fall 2015LabView
- Lecture All Matlab Slides · Fall 2015LabView
- Syllabus · Fall 2015LabView
- Lecture All C++ Lectures · Fall 2015LabView
- Lecture All Matlab Lectures · Fall 2015LabView
- Lecture First Textbook PDF · Fall 2015LabView
- Lecture Second Textbook PDF · Fall 2015LabView
Show the text of this lab
/*
* course: EECE 231
* Lab: 2
* Problem: 1
* author: Ameen Jaber
*/
#include <iostream>
using namespace std;
int main() {
// Declare variables
int i,j,k;
bool expA, expB, expC, expD, expE;
// Read values for i,j, and k from user
cout << "Please enter values for i,j, and k: ";
cin >> i >> j >> k;
// Compute the expressions
expA = i<5;
expB = (i>= 5) && (i<=10);
expC = (i>0) || (j%5 == 0);
expD = (i>0) || (j>=0 && (j%5 == 0));
expE = (i == j && j == k) || (i != j && j != k && i != k);
// Print the values of the expressions
cout << "The value of expression A is: " << expA << endl;
cout << "The value of expression B is: " << expB << endl;
cout << "The value of expression C is: " << expC << endl;
cout << "The value of expression D is: " << expD << endl;
cout << "The value of expression E is: " << expE << endl;
return 0;
}
//Problem Assignment 2, Fall 14/15
//Problem 2
//Author: Maya Safieddine
#include <iostream>
#include <cmath> //to use the pow function
using namespace std;
int main()
{
double a, b, c;//declare the coefficient variables of the quadratic
equation
cout<<"Please enter the values of a, b, and c"<<endl;
cin>>a >> b>> c;//prompt the user to enter the values of variables a, b,
and c
double discriminant = pow(b,2.0)-4*a*c;//compute the discriminant value
into variable of type double (since expected value of the equation is decimal)
//we use 2.0 in pow function to remove ambiguity, i.e. selcting the pow
function that expects double inputs. The pow function is defined in the cmath
library
cout<<"discriminant: "<<discriminant<<endl;
if(discriminant ==0)
{
cout<<"single repeated root:"<<endl;
double root = -b/(2*a);//compute the single root
cout<<root<<endl;
}
else if(discriminant < 0)
{
cout<<"two complex roots"<<endl;
}
else
{
cout<<"two real roots:"<<endl;
double root_1 = (-b + pow(discriminant,0.5))/(2*a);//compute the two
real roots
double root_2 = (-b - pow(discriminant,0.5))/(2*a);
cout<<root_1<<" and "<< root_2 <<endl;
}
//test example1: 1 1 1 two complex roots, discrimint: -3, two complex
roots
//test example2: 1, 2, 1 single real, discrimint:0, root: -1
//test example3: 1, 5, 1 two real roots: -4.79, -0.208, discrimint:21
return 0;
}
//Problem Assignment 2, Fall 14/15
//Problem 3
//Author: Maya Safieddine
#include <iostream>
using namespace std;
int main()
{
cout << "Enter the number of integers in the list: ";
int n;
cin >> n;
int count = 1;// counter to keep track of the number items
int number; // to store the next user input
int max, min; // to store the max and the min
cout << "Enter "<< n<<" integers: ";
while ( count<=n) //this condition indicates when to stop looping over the
loop body.
//Since we are reading a number at every loop, exit when all numbers
are read and processed i.e. n numbers.
//count tracks the number of read numbers. Since count is initially
set to 1, stop when count reaches n -> loop while count< n+1 (or equivalently
count<=n)
{
cin >> number; // read number from the user
if(count == 1) { // if count ==1, i.e., we are looking at the first
number, we initialize max and min to this number, as it's the only number
max = number;
min = number;
}
else { //count>1, a new number is added other than first
if (number<min)//if this number is less than min
min = number; //set min to the number
if (number>max)//if this number is greater than max
max=number; //set max to this number
}
count++;//increment count
}
if (n != 0) {//if the list is not empty
cout << "The largest number: " << max <<endl;
cout << "The smallest number: "<< min <<endl;
}
else
cout << "No input." << endl;
return 0;
}
///Assignment 2-Problem 4 Solution
//Ali Yassin
#include <iostream>
using namespace std;
int main()
{
int n;
int numOfEven=0;
int sumOfEven=0;
int x;
cout<<"Enter the number of integers in the list please."<<endl;
cin>>n;
cout<<"Enter an integer"<<endl;
for(int i=0;i<n;i++)
{
cin>>x;
if(x%2==0){
numOfEven++;
sumOfEven=sumOfEven+x;
}
}
cout<<"The number of the even integers in the list is: "<<numOfEven<<endl;
cout<<"The sum of the even integers in the list is: "<<sumOfEven<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cout <<"This is a primality test. Please enter a positive integer:"<<endl;
cin >> n;
bool isPrime = true;
if (n==1)
isPrime = false; // 1 is not prime by convention
else {
int i = 2;
while(i<= n-1&& isPrime==true) {
if( n%i == 0)
isPrime =false;
i++;
}
}
if(isPrime)
cout <<endl<<n<<" is prime"<<endl;
else
cout <<endl<<n<<" is not prime"<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cout <<"This is a primality test. Please enter a positive integer:"<<endl;
cin >> n;
bool isPrime = true;
if (n==1)
isPrime = false; // 1 is not prime by convention
else {
int i = 2;
while(i*i<=n && isPrime==true ) { // "i*i<=n" is equivalent to
"i<=square_root(n)"
if( n%i == 0)
isPrime =false;
i++;
}
}
if(isPrime)
cout <<endl<<n<<" is prime"<<endl;
else
cout <<endl<<n<<" is not prime"<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
cout<<"Please enter a positve integer: ";
cin>> x;
int numberOfPrimes = 0;
int n=2;
while(n<=x)
{
//////////// code from part (b) /////////////
bool isPrime = true;
int i = 2;
while(i*i<=n && isPrime == true) { // "i*i<=n" is equivalent to
"i<=square_root(n)"
if( n%i == 0)
isPrime =false;
i++;
}
////////// end of code from part (b) /////////
if(isPrime) numberOfPrimes ++;
n++;
}
cout<<endl;
cout<<"f("<<x<<")= "<<(numberOfPrimes+0.0)/x<<endl;
return 0;
}