Professional Documents
Culture Documents
EE0107-19 Basic Programming and Lab: Sebelas Maret University
EE0107-19 Basic Programming and Lab: Sebelas Maret University
EE0107-19 Basic Programming and Lab: Sebelas Maret University
Labwork Manual:
EE0107-19 Basic Programming and Lab
#include <iostream>
Using namespace std;
int main ()
{
cout<<"Welcome to C++
Programming\n"<<endl;
return 0;
}
Student Name :
ID Number :
Class :
Update: 20/9/2020
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Labwork Outline
Lab 1 : Introduction
This first lab offers an introduction for writing and executing C++ programs using
CodeBlocks. This lab also introduces cout instruction and software life cycle.
Lab 4 : Looping
In this lab, students explore basic looping commands through using( FOR loop.
DO…While… etc)
Lab 6 : Functions
Students explore how to create their own function.
Lab 7 : Array
In this lab, students will learn how to build a matrix using array.
Lab 8 : String
In this lab, student will learn about string and string manipulation in C++
programming..
2
L0107-19 – Basic Programming and Lab, Sebelas Maret University
3
L0107-19 – Basic Programming and Lab, Sebelas Maret University
6- If you have an error try to fix and debug it. Don’t worry if you have an error, it is usual
during programming.
7- You may modify your code, such as following and then re-build and re-run it.
//Lab01:Instroduction
#include <iostream>
using namespace std;
int main()
{
cout << "Hi My Name is XXX!" << endl;
cout << "I'm a C++ Programmer Now" << endl;
return 0;
}
4
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Variables in C:
Name Description Size* Range*
signed: -128 to 127
char Character or small integer. 1byte unsigned: 0 to 255
Boolean value. It can take one of two
bool values: true or false. 1byte true or false
short signed: -32768 to 32767
int (short) Short Integer. 2bytes Unsigned 0 to 65535
long signed: -2147483648 to 2147483647
int (long) Long integer. 4bytes unsigned: 0 to 4294967295
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
Double precision floating point
double number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long Long double precision floating point
double number. 8bytes +/- 1.7e +/- 308 (~15 digits)
String 4bytes
int main ()
{
// declaring variables:
int a, b;
int result;
// Initialization:
a = 5;
b = 2;
// process:
a = a + 1;
result = a - b;
5
L0107-19 – Basic Programming and Lab, Sebelas Maret University
//Lab02b:Instroduction
#include <iostream>
using namespace std;
int main()
{
/* Declaration */
int i;
bool hasil;
float x;
double y;
char q;
string z;
/* Initialization */
i=325;
x=345.65;
y=5.010210210678;
q='Nama';
z="Nama";
/* Printing to screen */
cout<<endl<<"i="<<i;
hasil = i > 320;
cout<<endl<<"hasil="<<hasil;
//boolean menghasilkan output 1 true atau 0 false
cout<<endl<<"x="<<x;
cout<<endl<<"y="<<y;
cout<<endl<<"q="<<q;
cout<<endl<<"z="<<z;
}
Exercise 03:
This program uses some basic and compound mathematical operations.
#include <iostream>
#include <iomanip> //to use setprecision
//#include <stdio.h>
using namespace std;
int main(){
int a=10,b=3,c,d,e,f,g;
float y,z;
/* Basic operations */
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
cout<<"C="<<c<<"\t"<<"D="<<d<<"\n"<<"E="<<e<<"\
t"<<"F="<<f<<"\n"<<"G="<<g<<"\n"<<endl;
/* Compound Operations */
c+=a;
d-=b;
e*=a;
f/=b;
g++;
cout<<"C="<<c<<"\t"<<"D="<<d<<"\n"<<"E="<<e<<"\
t"<<"F="<<f<<"\n"<<"G="<<g<<"\n"<<endl;
/* Integer/float: is the result correct? */
y=b/2.3;
cout<<"Y="<<y<<"\n"<<endl;
/* Operator Precedence: what is the order of the operations? */
z=b/2.3;
cout<<setprecision(4)<<"Z="<<z<<endl;
}
6
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01:
Learn how to make IF statement in C++.
//Lab 3a: IF statement
#include <iostream>
using namespace std;
int main()
{
string nim,mynim="I0719001";
cout << "Enter NIM!" << endl;
cin>> nim;
if (nim==mynim)
{cout<<"This is your own NIM"<<endl;}
return 0;
}
Exercise 02:
Learn how to make IF… ELSE statement in C++.
//Lab 3b: IF... ELSE statement
#include <iostream>
using namespace std;
int main()
{
string nim,mynim="I0719001";
cout << "Enter NIM!" << endl;
cin>> nim;
if (nim==mynim)
{cout<<"This is your own NIM"<<endl;}
else {
{cout<<"This is not your NIM"<<endl;}
}
return 0;
}
Task 01:
Make a C++ program to detect odd and even number!
7
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 03:
Learn how to make nested IF statement in C++.
Exercise 04:
Learn how to make SWITCH statement in C++.
int main() {
int x;
cout << "Enter your choice:" << endl;
cin>> x;
switch (x)
{
case 1:
cout << "Choice is 1";
break;
case 2:
cout << "Choice is 2";
break;
case 3:
cout << "Choice is 3";
break;
default:
cout << "Choice other than 1, 2 and 3";
break;
}
return 0;
}
8
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01:
Simple FOR loop:
Exercise 02:
Simple WHILE loop:
Task 01:
Make nested loop for exercise 1!
9
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 03: (Introduce “goto” function, combine control and loop structure, etc)
1
Calculate the sum of series fraction numbers ( ), for n<100!
n2
N
1 1 1 1
2
1 ... ...?
n 1 n 4 9 N2
#include <iostream>
#include <iomanip>
#include <cmath>
#define PI 2.0*asin(1.0)
using namespace std;
int main()
{
int n ;
double x , term, sum , exact_sum;
sum = 0.0 ;
exact_sum = PI*PI/6.0 ;
lower: cout<<"Enter number of terms N = ";
cin>>n;
cout<<x<<"\t"<<setw(10)<<term<<"\t"<<setw(8)<<sum<<"\n"
;
}
cout<<"\n\nFor N = infinity, Sum = "<<exact_sum<<endl;
}
Task 02:
Modify the above program using if-else statement and do while looping!
10
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Pre-lab: each student or group of student are asked to bring a simple engineering problem then
write the program in the class.
Exercise 1: Exercise 2:
write a C++ program to evaluate the function Read and average 6 integers using while loop, print the
written below. result.
#include <iostream>
2
x x for ( x 0) using namespace std;
x
int main()
y 1 e for ( 10 x 0) {
int x;
x 5 for ( x 10)
int count = 0;
double sum = 0;
#include <iostream> double average;
using namespace std;
int main() // prompt the user:
{ double x,y; cout << "Enter six grades separated by a single
space, then press <Enter>: ";
cout << " Enter the value of x"; while( count < 6)
cin >> x; {
cin >> x;
if (x >= 0 ) sum = sum + x;
y= x*x + sqrt(x); count++;
else if ( x > -10 && x < 0 ) }
y= 1 - exp (x); cout << endl;
else
y= fabs ( x + 5 ); average = sum/6;
cout << "The average is " << average << endl;
cout << " y = " <<y<<endl;
return 0;
return 0; }
}
11
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Mesh analyzis:
(R1+R2)i1-R2i2+0i3=V1
-R2i1+(R2+R3+R4)i2-R4i3=0
0i1-R4i2+(R4+R5)i3=-V2
2i1-i2+0i3=5
-i1+3i2-i3=0
0i1-i2+2i3=-5
12
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01:
Make a program to calculate factorial (n!) and binomial coefficient, defined by:
#include <iostream>
using namespace std;
int main()
{
int factorial(int);
int p,n,k, result1,result2;
// computing factorial ( max number 12!)
cout<<"Enter number to be factorial(ex: 8 for 8!): ";
cin>>p;
result1=factorial(p);
cout<<p<<"!="<<result1<<endl;
// Binomial calculation
cout<<"Enter number for (n) of binomial cooficient: ";
cin>>n;
cout<<"Enter number for (k) of binomial cooficient: ";
cin>>k;
result2=factorial(n)/(factorial(k)*factorial(n-k));
cout<<result2<<endl;
}
int factorial(int n)
{
int i,r=n;
for (i=n;i>1;i--)
{
r=r*(i-1);
}
return r;
}
Task 01:
Create a your own C++ program that contain at least one user-defined function!
13
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01:
Make a program to create a 3x3 matrix by using an array!
#include <iostream>
using namespace std;
int main()
{
int i,j;
int a[3][3]= {1,2,3,10,20,30,100} ;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
Task 01:
Create a your own C program that mutiply two matrix (C=AxB)! The dimension of the matrices
is up to you. Example: calculate C=AxB, for the following matrices.
14
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01: Write a C++ program to read your name string from terminal! We may use getline
(read whole line) or use cin (read only one word).
#include <string>
#include <iostream>
using namespace std;
int main()
{
string name1,name2;
cout<<"Enter name: ";
getline (cin, name1);
cout<<"Your name is "<<name1;
15
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Objective: In this lab, student will study reading from and writing to external files.
Exercise 01:
Write a program that converts degrees Celsius (TC) to degree Fahrenheit (TF) (Recall that TF =
(9/5) TC + 32). The program reads TC from input file “input.txt” and writes TF to the file
“output.txt”. Note: before running your program, add the input file first!
while(!inputfile.eof()){
inputfile>> TC;
TF = (9/5.0)*TC+32;
outputfile<<TC << "\t" <<TF<<endl;
}
Task 01:
If the file (input.txt) contains the following data. The first column is voltage and the second column is the electric
current.
3.0 2.1
1.5 1.1
2.6 4.1
Write program that reads the voltages and currents then calculates the electric power (P) based on the equation:
P=v*i
Write your output to the file (output.txt) with voltage in the first, current in the second and power on the third
column. Your output file should look like:
16
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01: This program give simple example of pointer in C++! Pointers store address of
variables or a memory location.
#include <iostream>
using namespace std;
int main () {
int var = 20; // Variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int data[5], i;
cout<<"Enter elements: ";
17
L0107-19 – Basic Programming and Lab, Sebelas Maret University
#include <iostream>
using namespace std;
struct datamahasiswa
{
char nama[50];
char asal[50];
int tahun;
};
struct datamahasiswa s[5];
int main()
{
int i;
for (i=0;i<5;i++)
{
cout<<"\nMasukkan informasi Mahasiswa ke-:"<<i;
cout<<"\nMasukkan nama: ";
cin>>s[i].nama;
cout<<"Enter asal: ";
cin>>s[i].asal;
cout<<"Enter tahun: ";
cin>>s[i].tahun;
}
cout<<"\nNama 1: "<<s[0].nama;
cout<<"\nNama 2: "<<s[3].nama<<endl;
return 0;
}
Task 01: Use record to read and save data from a file containing list of student ID and their final
exam scores. Allow user to display score from a selected student.
18