{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture2 - Monday,Oct11 #1 Moreaboutvariables Newoperators...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Monday, Oct 11 th Questions about Project #1? Week 1 review challenge A potpourri of new topics More about variables New operators (+=, ++, etc.) The if statement String variables…
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Time for your favorite game! Programming Language Inventor  Or Serial Killer See if you can guess who uses a keyboard and who uses a  chainsaw!
Image of page 2
Review Challenge This program computes the average of 2 numbers and prints the result. Find  the syntax & semantic errors. #include <iostream> int main(void) { int sum, a, b; std::cout << “Enter 2 numbers:\n“; std :: cin >> a; sum = sum + a; std::c in >> b sum = sum + b; sum = sum / 2; std::cout >> “Average: “ + sum; ; )
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Review Challenge #include <iostream> int main(void) { int n; std::cout << “Enter n: “; std::cin >> n; // add your code here std::cout << ;        std::cout << ; std::cout << ; } Given a number,  n , in seconds, print out the number of hours, minutes and  seconds that n represents. Hint : Use regular division and  modulo division E.g. if the user types in 7265, the  program should print: Hours: 2 Minutes: 1  Seconds: 5
Image of page 4
More About Variables Learn how to use char  variables. Learn how to initialize more  than one variable at a time Learn about const variables Learn some new C++  operators Linda writes her first C++  program…
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
char variables int main(void) { char small_num; small_num = -125; // this is OK! Last time we learned that char  variables can hold numbers  between -127 and 128. To assign a symbolic value to a character variable, use  single quotation  marks  ‘ and ’. You print out character variables just like you do any other variable.  char variable  can also hold a  symbolic character, like a letter  or a punctuation mark. char grade, punct; grade = ‘ A ’; punct = ‘ $ ’; cout << “Your grade is: “ << grade; }
Image of page 6
char variables #include <iostream> int main(void) { char grade; cout << “Enter desired grade: “; cin >> grade; cout << “You don’t deserve a “ << grade; } You can also prompt  the user to input a  single character using  std::cin . The user can type in  a single character  and then hit the  enter key. grade        ‘P’ Enter desired grade: B ‘B’ You don’t deserve a B
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
#include <iostream> int main(void) { int a, b, c; a = b = c = 5; } Assigning Multiple Variables at The  Same Time Sometimes its convenient to assign many variables to  the same value at once. This is how you do it. #include <iostream> int main(void) { int a, b, c; c = 5; b = c; a = b; } And here’s how C++ treats it.
Image of page 8
Const Variables What’s wrong with this program? #include <iostream> int main(void) { double rad; std::cout << “Enter the radius of your zit: “; std::cin >> rad; std::cout << “Zit circumference: “ << 2 * rad * 3.141; std::cout << “Zit area: “ << rad*rad * 3.141; } Hint: 1.What do we have to do to our program         to get increased precision (3.141 ->3.14159)?
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Const Variables #include <iostream> int main(void) { const double PI = 3.14;
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern