Lab Week 1

advertisement
Objected Oriented Programming
(COM326M1)
Lab Week 1
Using Visual Studio
The Visual Studio Integrated Development Environment (IDE) offers a set of tools
that help you write and modify code, and also detect and correct errors. In these
topics, you create a new standard C++ program and test its functionality by using
features available in Visual Studio for the C++ developer.
Getting started
1.
Lunch Visual Studio
2.
Select File  New  Project
3.
Click Win32 from Visual C++ under Project Types
4.
Select Win32 Console Application on the right hand side under Templates.
5.
Type a name for the project in the Name field, e.g. lab1 or week1.
When you create a new project, Visual Studio puts the project in a solution.
Accept the default name for the solution, which is the same name as the project.
You can accept the default location, type a different location, or browse to a
directory where you want to save the project.
Press OK to start
6.
You will now see the Win32 Application Wizard.
7.
On the left side you will find the link called Application Settings and on the
right side Application type
8.
Select Console Application. Select the Empty Project setting under
Additional options and click Finish.
9.
You now have created a solution – a solution is composed of one or more
projects. You now have a project without source code files.
10. Now select File  New  File
11. New File dialog box appears.
12. Select Visual C++ from Categories and C++ File from Templates and press
Open button
13. By default, the name of your new file is called Source1.cpp, Change it to a
new name, say lab1.cpp or week1.cpp and save it.
14. Now go to File menu option and select the Move lab1.cpp  1 lab1 project
15. You are now in the Editor, and the file lab1.cpp has been added to this
project.
16. Type the following code into the editor
#include <iostream>
using namespace std;
int main( )
{
cout<<”-----------------------”<<endl;
cout<”This is your first program in C++”<<endl;
cout<<”-----------------------”<<endl;
cout<<”Congratulation on the success!”<<endl;
return 0;
}
17. Once done, under Debug menu option, select Start without Debugging
18. You may see a dialog box appear, you will be asked if you would like to
build your application- click Yes button.
19. Output window at the bottom of Visual Studio provides status information.
20. At the end of this process, the application will lunch a new console
application and display the result of your program if there is no error.
21. To terminate the console, Press any key
Variables, Constant, Operators, Expressions, and I/O
Exercise 1: Check which valid variables, constants, Operators and
expressions are. Check the program, correct the errors and compile it and get
the results. It will display the value of count and velocity!
#include <iostream.h>
using namespace std;
void main()
{
int count;
float time=10.0, mass=9.9;
double distance, force, v0=1.0, acceleration=5.0;
cin >> mass;
force = mass * acceleration;
count = (70-5 % 3 * 4 + 9);
count++;
count--;
++count;
--count;
cout << count;
//count=count+1
//same as count++
//count=count-1;
//same as count--;
distance = v0*time+0.5*acceleration*(time*time);
velocity = distance / time;
cout << velocity <<”\n”;
}
Exercise 2
#include <iostream>
using namespace std;
int main( )
{
int alpha, A;
float beta,B;
char X;
cout<< “---------------------------------“<<endl;
cout<<”Enter value for alpha, beta: “<<endl;
cin>>alpha>>beta;
A=alpha; B=beta;
cout<<”Enter a character in lower case: “<<endl;
cin>>X;
cout<< alpha << “ “ << beta <<\n”;
cout<<“The sum of “<<A<<“and”<<B<<“ is“<<A+B<< ”\n” ;
cout<< “-----------------------------“<<endl;
cout<<toupper(X)<<endl;
return 0;
}
Exercise 3
Leonhard Euler discovered the famous prime-generating formula in 1772. The
prime numbers on the marked curve have the form x2 + x + 41.
#include <iostream>
using namespace std;
void main( )
{
int x, p;
cout<< “-----------------------------“<<endl;
cout<<”Enter value for x : “ <<endl;
cin>>x;
p= x*x+x+41 ;
cout<< “The prime number is:“<<endl;
cout<<p <<endl;
cout<< “-----------------------------“<<endl;
}
Write the programs in Exercise 2 and 3, compile, debug and run the program.
Once the programs are running, show it to module coordinator/demonstrator
for records. Save the program in folder on your U drive. You will need the
programs for the assignment/open book lab test in week 10.
Number spiral
Number spirals are very simple. To make one, we just write the non-negative
integers on a ribbon and roll it up with zero at the centre. The trick is to
arrange the spiral so all the perfect squares (1, 4, 9, 16, etc.) line up in a row
on the right side:
If we continue winding for a while and zoom out a bit, the result looks like
this:
If we zoom out even further and remove everything except the dots that
indicate the locations of integers, we get the next illustration. It shows 2026
dots:
Let's try making the primes darker than the non-primes:
The primes seem to cluster along certain curves. Let's zoom out even further
to get a better look. The following number spiral shows all the primes that
occur within the first 46,656 non-negative integers. (For clarity, non-primes
have been left out.)
It looks as though primes tend to concentrate in certain curves that swoop
away to the northwest and southwest, like the curve marked by the blue arrow.
Leonhard Euler discovered the famous prime-generating formula in 1772. The
prime numbers on the marked curve have the form x2 + x + 41.
Download