EECE 230 Exam 1 Solution, Fall 2013
American University of Beirut · Introduction to Programming · 6 pages · Dr. Louay Bazzi · with solution
More EECE 230 files
- Exam 2 Solutions · Spring 2019Exam · with solutionView
- Exam 1 Solutions from Dr.Bazzi · Spring 2019Exam · with solutionView
- Exam 1 Quiz 1 Spring 18 · Spring 2019ExamView
- Exam 2 · Spring 2018ExamView
- Exam 1 · Spring 2018ExamView
- Exam 2 Quiz 2 Collection with Solution · Spring 2017Exam · with solutionView
- Exam 2 Quiz 2 2016 · Spring 2017ExamView
- Exam 1 Quiz 1 with Solution Bazzi · Spring 2017Exam · with solutionView
Show the text of this exam
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<< "Please enter three integers: ";
cin >>a>>b>>c;
int num=0;
if(a>0) num=num+1;
if(b>0) num=num+1;
if(c>0) num=num+1;
cout<<"Number of positive integers:"<<num<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int n;
cout<< "Please enter the number of intgers:";
cin>>n;
if(n<0)
cout<<"Number of integers negative!"<<endl;
else {
if(n>0)
cout<<"Please enter "<<n<<" intgers:";
int num=0,x;
for(int i=0;i<n;i++)
{
cin>>x;
if(x>0) num++;
}
cout<<"Number of positive integers:"<<num<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int A[100];
int number;
cout << "Enter a list of integers ending with " <<-999<<":"<<endl;
cin >> number;
int i = 0;
while (number != -999) {
A[i]= number;
i++;
cin >> number;
}
int n = i;
////////////////////////////////////////////////////////
bool found = false;
for(i = 2; i< n && !found;i++)
if(A[i-2]+A[i-1]+A[i]==0)
found=true;
if(found)
cout<<"YES";
else
cout<<"NO";
cout<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
bool found = false;
int number;
cout << "Enter a list of integers ending with" <<-999<<":"<<endl;
cin >> number;
int i = 0;
int previous=0;
int previous2=0;
while (number != -999) {
i++;
previous2 = previous;
previous = number;
cin >> number;
if(i>=2 && number+previous+previous2==0)
found = true;
}
if(found)
cout<<"YES";
else
cout<<"NO";
cout<<endl;
return 0;
}
#
#include<iostream>
using namespace std;
const int SENTINEL = -999;
int main()
{
////////////////////////////////////////////////////////
/// read the integers //////////////////////////////////
int A[100];
int number;
cout << "Enter a list of integers ending with " <<SENTINEL <<":"<<endl;
cin >> number;
int i = 0;
while (number != SENTINEL) {
A[i]= number;
i++;
cin >> number;
}
int n = i; // Number of integers in the list
////////////////////////////////////////////////////////
int maxLength = 0;
for(i=0;i<n;i++)
{
int j=i;
while(j<n-1&& A[j+1]>A[j])
j++;
if(j-i+1>maxLength)
maxLength=j-i+1;
}
cout<<endl<<"Length of longest increasing subsequence: "<<maxLength<<endl;
////////////////////////////////////////////////////////
return 0;
}
#include<iostream>
using namespace std;
const int SENTINEL = -999;
int main()
{
cout << "Enter a list of integers ending with " <<SENTINEL <<":"<<endl;
int x;
cin >> x;
int maxLength = 0;
int length= 0;
int previous = 0;
while (x != SENTINEL) {
if(length==0)
length = 1;
else{
if(x>previous) length++;
else {
if(length>maxLength) {
maxLength = length;
length=1;
}
}
}
previous = x;
cin >>x;
}
if(length>maxLength) // in case the longest increasing subsequence ends
with the input
maxLength = length;
cout<<endl<<"Length of longest increasing subsequence:
"<<maxLength<<endl;
////////////////////////////////////////////////////////
return 0;
}