Programming Manual For Basic Learning
Programming Manual For Basic Learning
Labwork Manual:
EL0107-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: 7/10/2019
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 printf() 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 9 : Pointer in C
In in this lab, students will learn how to build a pointers and other.
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:
9
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 03:
1
Calculate the sum of series fraction numbers ( ), for n<100!
n2
N
1 1 1 1
n
n =1
2
= 1+ + + ... + 2 = ...?
4 9 N
#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 01:
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>
x 2 + x for ( x 0) using namespace std;
int main()
y = 1 − e x for (−10 x 0) {
x+5 for ( x −10) int x;
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.
Exercise 02:
14
L0107-19 – Basic Programming and Lab, Sebelas Maret University
#include <iostream>
using namespace std;
int main()
{
char name[20];
cout<<"Enter name: ";
cin>>name;
cout<<"Your name is "<<name;
return 0;
}
Exercise 03:
Combining two strings!
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strncat(s1,s2, 3);
cout<<"Concatenation using strncat: "<<s1<<endl;
return 0;
}
Task 02:
Explore more about string operations (copying, etc...)!
15
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01:
Write the following program, then compile and execute. Try to modify the program.
//Lab08:Input and Output
# include <iostream>
using namespace std ;
int main ()
{
char pilihan ;
cout << " Main Menu " ; cout << endl << endl ;
cout << " 1. Operasi Penjumlahan " ; cout << endl ;
cout << " 2. Operasi Pengurangan " ; cout << endl ;
cout << " 3. Operasi Perkalian " ; cout << endl ;
cout << " 4. Operasi Pembagian " ; cout << endl << endl ;
cout << " Masukkan Pilihan Anda : " ;
cin >> pilihan ;
switch ( pilihan ) {
case '1' :
float satu,dua,jumlah ;
cout << endl ;
cout << " Operasi Penjumlahan " ; cout << endl << endl ;
cout << " Masukkan Nilai Pertama = " ;
cin >> satu ;
cout << " Masukkan Nilai Kedua = " ;
cin >> dua ;
jumlah = satu + dua ;
cout << " Hasil Penjumlahan Dua Buah Bilangan Tersebut Adalah " << jumlah ;
cout << endl << endl ;
break ;
case '2' :
float tiga,empat,kurang ;
cout << endl ;
cout << " Operasi Pengurangan " ; cout << endl << endl ;
cout << " Masukkan Nilai Pertama = " ;
cin >> tiga ;
cout << " Masukkan Nilai Kedua = " ;
cin >> empat ;
kurang = tiga - empat ;
cout << " Hasil Pengurangan Dua Buah Bilangan Tersebut Adalah " << kurang ;
cout << endl << endl ;
break ; 16
case '3' :
float lima,enam,kali ;
cout << endl ;
L0107-19 – Basic Programming and Lab, Sebelas Maret University
case '2' :
float tiga,empat,kurang ;
cout << endl ;
cout << " Operasi Pengurangan " ; cout << endl << endl ;
cout << " Masukkan Nilai Pertama = " ;
cin >> tiga ;
cout << " Masukkan Nilai Kedua = " ;
cin >> empat ;
kurang = tiga - empat ;
cout << " Hasil Pengurangan Dua Buah Bilangan Tersebut Adalah " << kurang ;
cout << endl << endl ;
break ;
case '3' :
float lima,enam,kali ;
cout << endl ;
cout << " Operasi Perkalian " ; cout << endl << endl ;
cout << " Masukkan Nilai Pertama = " ;
cin >> lima ;
cout << " Masukkan Nilai Kedua = " ;
cin >> enam ;
kali = lima * enam ;
cout << " Hasil Perkalian Dua Buah Bilangan Tersebut Adalah " << kali ;
cout << endl << endl ;
break ;
case '4' :
float tujuh,delapan,bagi ;
cout << endl ;
cout << " Operasi Pembagian " ; cout << endl << endl ;
cout << " Masukkan Nilai Pertama = " ;
cin >> tujuh ;
cout << " Masukkan Nilai Kedua = " ;
cin >> delapan ;
bagi = tujuh / delapan ;
cout << " Hasil Pembagian Dua Buah Bilangan Tersebut Adalah " << bagi ;
cout << endl << endl ;
break ;
default :
cout << " Anda Salah Pilihan " ;
cout << endl ;
}
return 0 ;
}
Exercise 02:
1. Change the previous program to read from an input file. Do the following:
17
L0107-19 – Basic Programming and Lab, Sebelas Maret University
#include <stdio.h>
#include <stdlib.h>
void main (void)
{
/* declaration */
int a;
float x;
double y;
FILE *in;
in=fopen("inputfile.txt","r");
printf("Enter a (interger)=");
fscanf(in,"%d",&a);
printf("a=%d\n", a);
printf("Enter x (float)=");
fscanf(in,"%f",&x);
printf("x=%f\n", x);
printf("Enter y (double)=");
fscanf(in, "%lf",&y);
printf("y=%lf\n", y);
}
2- Now change the program such that it writes the output to an external file called outputfile.txt
by doing the following:
- Change the file pointer declaration to: FILE *in, *out;
- Add after the declaration part: out=fopen("outputfile.txt","w");
- Change each printf("…",..); instruction to fprintf(out,"…",..);
18
L0107-19 – Basic Programming and Lab, Sebelas Maret University
Exercise 01:
This program give simple example of pointer in C!
//Lab09:Pointer
#include <iostream>
using namespace std;
int main()
{
int Var = 5;
int *VarPoint; // It is said that VarPoint is of type ' int * ' which stores an address.
VarPoint = &Var;// & unary operator returns the address of the variable.
// In General the statement : VarPoint = Var; is an error.
// Because VarPoint is of type ' int * ' but Var is of type ' int ' and these are not convertable to
each other.
cout << "The VALUE which is pointed by VarPoint is : " << *VarPoint << endl;
// * unary operator returns the value of an address. See the difference in these 2 statements.
cout << "The ADDRESS which is pointed by VarPoint is : " << VarPoint << endl;
cout << "The address of VarPoint itself is : " << &VarPoint << endl << endl;
return 0;
}
Exercise 02:
Access array elements using pointers!
19
L0107-19 – Basic Programming and Lab, Sebelas Maret University
#include <iostream>
using namespace std;
int main()
{
int data[5], i;
cout<<"Enter elements: ";
Record/Structure:
#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;
}
20