Question

1. Type in the following program. Compile and run. #include <iostream> using namespace std; int main()...

1. Type in the following program. Compile and run.
#include <iostream>
using namespace std;
int main()
{
​cout << "Hello, world." << endl;
}
Open a Word Document and copy and paste the program and the output.
2. Create a new program:
#include <iostream>
using namespace std;
int main()
{
​float num1;
​float num2
​float avg;
​cout << "Enter two numbers" << endl;
​cin >> num1 >> num2;
​avg = num1 + num2/2;
​cout << "The average is " << avg;
}
3. Try to compile. There should be a syntax error. The compiler should give some indication of where the error occurred. Correct the error and compile again.
4. Run the program several times, entering different values. Examine the output carefully. Is it what you expect? There should be a logic error, or bug. See if you can correct it.
5. Test your program to make sure it functions as you expect. Submit your code, a sample output, and answers to the following questions.
Lab Questions
1. What is the role of the compiler?
2. What was the syntax error in the above program?
3. What was the logic error in the above program?
4. In one sentence, summarize the difference between syntax errors and logic errors.

Homework Answers

Answer #1

1 )

#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world." << endl;
}

OUTPUT :

2 )

#include <iostream>
using namespace std;
int main()
{
float num1;
float num2;
float avg;
cout << "Enter two numbers" << endl;
cin >> num1 >> num2;
avg = ( num1 + num2 )/2;
cout << "The average is " << avg;
}

OUTPUT:

1 ) Role of compiler is to find if there are any errors in the source code

2 ) float num2 ( ; missed )

3 ) num1 + num2/2 -- wrong

   ( num1 + num2 ) / 2;

4 ) syntax error is misplacement of the identifiers

logic error is the error that occurs due to the any fault in the programmic logic

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
What will be the output of the following code? #include <iostream> using namespace std; int main()...
What will be the output of the following code? #include <iostream> using namespace std; int main() {    char *s = "hello";    char *p = s;    cout << *(p+3) << " " << s[1] << endl; }
whats the output of the following program? #include <iostream> using namespace std; int main () {...
whats the output of the following program? #include <iostream> using namespace std; int main () { int fistNum = 28; int secondNum = 25; cout << firstNum << " " << secondNum << end1; cout << (firstNum = 38-7) << end1; cout << (firstNum <= 75) << end1; cout << (firstNum > secondNum + 10) << end1; cout << (firstNum >= 3 * secondNum - 100) << end1; cout << (secondNum - 1 == 2 * firstNum) << end1; cout...
C ++ #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile;...
C ++ #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt"); while (!inFile.eof()) { inFile >> bmi; if( bmi < 18.5) { cout << bmi << " is underweight " ; } else if( bmi >= 18.5 && bmi <= 24.9) { cout << bmi << " is in normal range " ; } else if( bmi >= 25.0 && bmi <= 29.9) { cout << bmi << " is overweight " ; }...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;     float f=2.22;     float h=-7.5;     char a='S';     char b='M';     cout<<i<<"\t"<<j<<"\t"<<k<<endl;     Cout<<f<<"\t"<<h<<endl;     cout<<a<<"\t"<<b<<endl;     return 0;     }
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std;...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std; int main() {        int x;        cout << "Selection option " << endl;        cin >> x;        if (x == 1)               cout << "You select option 1" << endl;        else if (x >= 2 || x <= 4)               cout << "You select options 2 or 3 or 4" << endl;               else if (x == 10)               cout << "You select option 10" << endl;               else...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Analyze the following program and write down the output. # include <iostream> using namespace std;    void...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void modifyArray( int [ ], int );    void modifyElement ( int );      int main( ) {   const int arraySize = 8;   int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };      modifyArray ( a, arraySize);      for ( int i =0; i < arraySize; i++)               cout << a[i] << ‘  ’;         modifyElement ( a[4] );          for ( int i =0; i <...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout << "Welcome to the Calorie Count-ulator!\n"; // Get user input: cout << "Enter your meal choice ([P]izza, [S]alad, [H]amburger)\n"; cin >> meal_choice; cout << "Enter the amount of servings (1-9):\n"; cin >> servings; // TODO: Use a switch statement to evaluate the user's meal choice // Handle error checking where appropriate // Exit the program: return 0;}
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT