27
CS 114 – Class 02 Topics Computer programs Using the compiler Assignments Read pages 21-38 for Thursday. We will go to the lab on Thursday

CS 114 – Class 02 Topics Computer programs Using the compiler Assignments Read pages 21-38 for Thursday. We will go to the lab on Thursday

Embed Size (px)

Citation preview

Page 1: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

CS 114 – Class 02 Topics

Computer programsUsing the compiler

AssignmentsRead pages 21-38 for Thursday.We will go to the lab on Thursday

Page 2: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Review

Algorithms Languages

Implementation: Visual Studio .NET

Page 3: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Software development process

Understand exactly what you are trying to do

(problem domain)

Figure out how you are going to do it(algorithm)

Code your solution

(programming)

Page 4: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

What is a computer program? Once you have figured out how to solve the

problem and have an algorithm Type in code (words) specific to a computer

languageHigh-level languages: C++, C, Java, etc.Lower-level languages: Assembly language

Must follow format, etc. for language

Page 5: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

How to run program Must execute the program on a computer

Language you use is converted (compiled) to machine code the computer can understand (bits – 1’s, 0’s)

This machine code is then run (executed) on the computer

Results produced from program (output) Print list of phone numbers Play video or music Create a new file on computer and put list of classes in

file Move robot around room

Page 6: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

How to run a program

We will use the high-level language C++ To execute the program we will use .NET,

Microsoft product that runs under windows (operating system)

.NET is an interactive environment In one step:

.NET will compile your program to machine code the computer can understand - then link/load and execute the program for you

Can test/debug in .NET

Page 7: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Visual Studio .NET Pro - Don’t have to worry about details, .NET

does it for you Cons -.NET has many features

too many for us to use in this classSometimes does things unexpectedly

Later in the semesterWe will use linux – Unix like operating systems

Page 8: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Evolution of running programs Old days – used punch cards

and mainframe Punch cards for your program Submit & wait several hours Repeat until correct

Today Interactive environment Permits immediate:

Program development Compile and execute Test and debug

Lots of environments We use: Microsoft Visual

Studio .NET 2005

Don’t use the system until you know how to solve the problem Must know the algorithm

Page 9: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Software development process

Understand exactly what you are trying to do

(problem domain)

Figure out how you are going to do it(algorithm)

Code your solution

(programming)

Page 10: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Today’s problem Microwave oven components

Control UnitHigh Voltage Unit

Control section accepts cook time from user Input is two numbers [minutes and seconds]

High Voltage Unit needs time in secondsNeed to convert two numbers representing

minutes (MM) and seconds (SS) to a total number of seconds to cook (TIME)

Our program: reads two numbers (MM & SS), prints one (TIME)

Page 11: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Microwave oven conversion

Make sure everyone at your table understands the problem

(What is the output with the input: 04 45?)

Program the solution. To do this:1. We need an answer to #2 above

2. We need to know how to use .NET

Write up an algorithm that explains precisely how to do this task

Page 12: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Write an algorithm to do the microwave problem

Page 13: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Microwave Algorithm

1. Read in minutes, store in MIN

2. Read in seconds, store in SEC

3. In a variable TIME, store the value( MIN x 60 ) + SEC

4. Print out the result

Page 14: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Almost ready to code a solution

Understand the problem Know an algorithm to solve it Next steps

Basic structure & format for a C++ programBasics of .NET (how to build a simple program)

Still need to know how to use C++ and .NET

Page 15: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Our algorithm in pseudo-code Begin program Print out (display) “enter minutes:” Read in (input) minutes from user

Where to put minutes? Print out (display) “enter seconds:” Read in (input) seconds from user Compute cook time Printout “total time” and cook time End program

Page 16: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Implementing our algorithm in C++Comments:

• All statements end with a semi-colon

• cin and cout are used for input and output

• One assignment statement computes our answer

• Statement can span multiple lines

•Use three variables to store values (integers)

int main ( ) {

int min, sec; cout << “Enter minutes : “; cin >> min; cout << “Enter seconds : “; cin >> sec; int time; time = min * 60 + sec; cout << “Total time is “ << time << endl;

return 0;

}

Page 17: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

First Program - Outputsint main ( ) { int min, sec;

cout<<"enter minutes:";cin >> min;cout<<"enter seconds:";cin >> sec;int time;time = min * 60 + sec;cout << "total time is " <<time << endl;return 0;

}

Output statements

Page 18: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

First Program - Inputsint main ( ) { int min, sec;

cout<<"enter minutes:";cin >> min;cout<<"enter seconds:";cin >> sec;int time;time = min * 60 + sec;cout << "total time is " <<time << endl;return 0;

}

Input statements

Page 19: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

First Program - Variables#include <iostream>using namespace std;int main ( ) { int min, sec;

cout << "enter minutes: ";cin >> min;cout << "enter seconds: ";cin >> sec;int time;time = min * 60 + sec;cout << "total time is " <<time << endl;return 0;

}

Variable declarations fromour first program.

Page 20: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Different types of containers hold different things

Integers(int)

Real numbers(double)

Characters(char)

Strings(later)

Variables

A Variable is a container Holds something needed in

your algorithm Variable names correspond

to a specific location in memory.

There are Naming Conventions for variables

For example, you can’t name a variable 5

Page 21: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Basic C++ Program Structure

Header:Loads basic librariesStandard conventions

Functions:Code/InstructionsMust include “main”may include others

#include <iostream>

using namespace std;

int main ( ) {

// statements return 0;

}

Page 22: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Put it all together…#include <iostream>using namespace std;int main ( ) { int min, sec;

cout << "enter minutes: ";cin >> min;cout << "enter seconds: ";cin >> sec;int time;time = min * 60 + sec;cout << "total time is " <<time << endl;return 0;

}

Header info

Page 23: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Microsoft Visual Studio .NET 2005 University

All CoE labs

Your own personal machines: Legal copy is free at

support.cs.ua.edu/ Click on Go to UA

MSDNAA Site

Username: [email protected]

PW: csPass06 Must give UA email

address

Page 24: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Microsoft Visual Studio .NET 2005 Visual Studio .NET

Designed for large, commercial applications

114 uses a small portion of its capabilities

Future classes use more features

Visual Studio C++ Express is smaller & free www.microsoft.com/

express/vc

Page 25: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Class Notes and Materials Log onto the computer

Userid = <first initial> <last initial> <last 4 digits CWID>

Big Al, CWID = 12345678userid = ba5678

Password = userid Domain = COE Change your password

Check your Bama mail!!

Page 26: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

Homework Homework #1 Due: Thurs. 8/28 before class

Send me e-mail so I will have everyone’s e-mail account in case of announcements

You don’t have to use your bama account Homework #2 Due: e-mail your .cpp file to

[email protected] Tues. 9/2 before classRun the program using the instructions at the link

belowPrint an additional line after hello that says goodbye

http://cs.ua.edu/~vrbsky/CS114/Instr.NET.htm

Page 27: CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages 21-38 for Thursday.  We will go to the lab on Thursday

End of Class 02

Assignments: Read pages 21-38 Get an Engineering computer account if you

don’t have one.