POINTER
REFERENCE PROGRAM
#include<iostream> //also &
can be used to get memory address of a stored variable //reference
name mji to refer to the dodoma variable: int main() { string dodoma ="makulu"; string &mji= dodoma; cout<<dodoma; cout<< &mji; } |
VARIABLE ADDRESS
#include<iostream> using namespace std; int main() { //also &
can be used to get memory address of a stored variable //when
variable is created memory address stored to that variable //and when
assigning value to variable its also stored in this address string &mji= dodoma; cout<<dodoma; cout<<mji; } |
MODIFYING POINTER
#include <iostream> using namespace std; int main() { string
food="ugali"; string* ptr= &food; cout<< food; //results normal
variable"ugali" cout<< &food; //result memory address cout<< ptr; //result the memory address of
variable food *ptr="wali"; //change the value from
variable food to be wali cout<< *ptr; //shows the changed variable |
SINGLE DIMENSION ARRAY
ARRAY/ CALENDER
// A C++ Program to Implement a Calendar // of an year #include<bits/stdc++.h> using namespace std; /*A Function that returns the index of the day of the date- day/month/year For e.g- Index Day 0 Sunday 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday*/ int dayNumber(int day, int month, int year) { static int t[] = { 0, 3, 2,
5, 0, 3, 5, 1, 4, 6, 2,
4 }; year -= month < 3; return ( year + year/4 -
year/100 + year/400 +
t[month-1] + day) % 7; } /* A Function that returns the
name of the month with a given month number Month Number Name 0 January 1 February 2 March 3 April 4 May 5 June 6 July 7 August 8 September 9 October 10 November 11 December */ string getMonthName(int monthNumber) { string months[] =
{"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" }; return
(months[monthNumber]); } /* A Function to return the number of days in a month Month Number Name Number of Days 0 January 31 1 February 28 (non-leap) / 29 (leap) 2 March 31 3 April 30 4 May 31 5 June 30 6 July 31 7 August 31 8 September 30 9 October 31 10 November 30 11 December 31 */ int numberOfDays (int monthNumber, int year) { // January if (monthNumber == 0) return (31); // February if (monthNumber == 1) { // If the year is leap
then February has // 29 days if (year % 400 == 0 || (year % 4 == 0
&& year % 100 != 0)) return (29); else return (28); } // March if (monthNumber == 2) return (31); // April if (monthNumber == 3) return (30); // May if (monthNumber == 4) return (31); // June if (monthNumber == 5) return (30); // July if (monthNumber == 6) return (31); // August if (monthNumber == 7) return (31); // September if (monthNumber == 8) return (30); // October if (monthNumber == 9) return (31); // November if (monthNumber == 10) return (30); // December if (monthNumber == 11) return (31); } // Function to print the calendar of the given year void printCalendar(int year) { printf (" Calendar - %d\n\n", year); int days; // Index of the day from 0
to 6 int current = dayNumber (1,
1, year); // i --> Iterate through
all the months // j --> Iterate through
all the days of the // month - i for (int i = 0; i < 12;
i++) { days = numberOfDays (i,
year); // Print the current
month name printf("\n ------------%s-------------\n", getMonthName
(i).c_str()); // Print the columns printf(" Sun
Mon Tue Wed
Thu Fri Sat\n"); // Print appropriate
spaces int k; for (k = 0; k <
current; k++) printf(" "); for (int j = 1; j <=
days; j++) {
printf("%5d", j); if (++k > 6) { k = 0;
printf("\n"); } } if (k)
printf("\n"); current = k; } return; } // Driver Program to check above funtions int main() { int year = 2016; printCalendar(year); return (0); } |
CONDITION STATEMENTS VS SWITCH CASE
WHILE LOOP/ AGE DETERMINATION
#include <iostream> using namespace std; int main() { int attempt=0; do{ int years;int
age; cout<<"ingiza mwaka wa kuzaliwa"; //suppose is 1990 cin>>years; cout<<"ingiza umri wa sasa"; //suppose 30 cin>>age; //calculate 2020-1990 is answer 30 ?? if(2020-years==age) { cout<<"mwaka wa kuzaliwa
na umri ni sawa"; //if true stop here break; } //if false continue here else { cout<<"umri
na mwaka wa kuzaliwa haviendani"; //repeat
until birthyear and age matches attempt
++; cout<<"umejaribu
mara ya="<<attempt; } } //excute here condition while(attempt<3); //but if
number of tries limit is 3, then do this.... if(attempt=3) { //continue; cout<<"umefikia
ukomo kujaribu mara 3"; } } |
NESTED IF INSIDE BOTH IF AND ELSE
#include<iostream> using namespace std; int main() { int x=3; int y=4; int z=12; int ans; if (x>=y) { if
(x==y) { ans=x+y; cout<<ans; } else { cout<<"not
equal to y"<<endl; } } else { if(x<=y) { ans=x*y; cout<<ans<<endl; } else { cout<<"x
is not less than or equal to y"<<endl; } } } |
NESTED IF INSIDE ELSE BLOCK
#include<iostream> using namespace std; int main() { int x=10; int y=20; int z; if (x>y) { z=x+y; cout<<z; } else { if(x<y) { cout<<"x<y"; } else { cout<<"not
true"; } } } |
NESTED IF STATEMENT INSIDE IF
#include<iostream> using namespace std; int main() { float results; int x=14; int y=20; int z=12; if (x<=y) { if (x>z) { results=x*y; cout<<results; } else results=x/y; cout<<results; } else results=x*y/z; cout<<results; } |
IF INSIDE IF GRADING SYSTEM PROGRAM
//if statement inside if #include <iostream> using namespace std; int main () { int
sumMark,avMark,markGrade,subject1,subject2,subject3,subject4; string
studentLevel; cout<<"what
is the level of student?"<<endl; cin>>studentLevel; if
(studentLevel=="formsix"||studentLevel=="FORMSIX") { cout<<"welcome
to grading system enter marks for student"<<endl; cout<<"enter
GS marks"<<endl; cin>>subject1; cout<<"enter
chemistry marks"<<endl; cin>>subject2; cout<<"enter
physics marks"<<endl; cin>>subject3; cout<<"enter
mathematics marks"<<endl; cin>>subject4; cout<<"total
marks=";
sumMark=subject1+subject2+subject3+subject4; cout<<sumMark<<endl; cout<<"avarage
marks="; avMark=sumMark/4; cout<<avMark<<endl; markGrade=avMark; cout<<"grade
is"; if
(markGrade>=0 &&markGrade<=20) { cout<<"F"; } else if
(markGrade>=21 &&markGrade<=29) { cout<<"E"; } else if
(markGrade>=30 && markGrade<=44) { cout<<"D"; } else if
(markGrade>=45 && markGrade<=60) { cout<<"c"; } else if
(markGrade>=61 && markGrade<=74) { cout<<"B"; } else if
(markGrade>=75 && markGrade<=100) { cout<<"A"; } else { cout<<"no
exam have been done"; } } else { cout<<"only
for form six"; } } |
SWITCH CASE INSIDE SWITCH
#include <iostream> using namespace std; int main () { int password=10; int id=2020; switch
(password) { case
10: switch
(id) { case
2020: cout
<<"correctly"; break; } } default: cout<<"either
one incorrect"; } |
FILE HANDLING
FILE HANDILING CREATE FILE MANUAL
#include<iostream> #include <fstream> using namespace std; int main() { ifstream infile; infile.open("data.txt"); int x; int y; int z; infile>>x>>y>>z; cout
<<"your x data is"<<x<<endl<<"your y
data is"<<y; } |
CREATE FILE MANUAL 2 DISPLAY NAMES
#include <iostream> #include <fstream> #include<string> using namespace std; int main() { ifstream
majinayangu; majinayangu.open("mynames.txt"); //check if file "mynames" exist if
(majinayangu.fail()) { cerr<<"no
such file!!!!!!"; exit(1); } //end file check string
firstname,middlename,lastname; majinayangu>>firstname; majinayangu>>middlename; majinayangu>>lastname; cout<<"your
names
are"<<firstname<<endl<<middlename<<lastname; majinayangu.close(); } |
CREATE FILE MANUAL ARRAY AND FUNCTION
//physical file named "data" value=2 #include <iostream> #include <fstream> #include<string> using namespace std; int jumlisha() { ifstream ongeza; ongeza.open("data.txt"); //check if file
exit if(ongeza.fail()) { cerr<<"no
such file"; exit(1); } int x; ongeza>>x; for ( x; x <=
5; x++) { cout<<x; } } int main() { int jumlisha(); jumlisha(); } |
CREATE FILE MANUAL WHILE LOOP
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string majina; int count=0; //read until you have reached the end ifstream mikoa; mikoa.open("data.txt"); while (!mikoa.eof()) { mikoa>>majina; count++; cout<<count<<"your
instances are"<<endl; } } |
CREATE FILE USER INPUT AND OUTPUT
#include<iostream> #include<fstream> #include<iostream> using namespace std; int a,b,c; int data1 () { ofstream data1; cout<<"input
two numbers"<<endl<<endl; cin>>a; cin>>b; data1.open("data.txt"); data1<<a<<b; cout<<"first_data
is"<<a<<endl; data1.close(); } int data2() { ifstream demo; demo.open("data.txt"); demo>>a>>b; cout<<"second_data
is"<<b<<endl; } int main() { data1(); data2(); } |
CREATE FILE CREATE ACCOUNT AND LOGIN
#include<iostream> #include<fstream> #include<iostream> using namespace std; int a,b,c; int data1 () { ofstream data1; cout<<"input
two numbers"<<endl<<endl; cin>>a; cin>>b; data1.open("data.txt"); data1<<a<<b; cout<<"first_data
is"<<a<<endl; data1.close(); } int data2() { ifstream demo; demo.open("data.txt"); demo>>a>>b; cout<<"second_data
is"<<b<<endl; } int main() { data1(); data2(); } |
SOLVING QUESTIONS
QUESTION
ONE.
(A)
Algorithm to display employee birth
year, retirement time and how many days remain. Imagine 55 years are minimal
retirement guarantee.
The operation is, the user enters current age, then
the age will be taken into considerations using arithmetic operations to calculate when he/she was born, how many
years left to retire and when retirement will begin
Step
1: start
Step
2 : enter your age say AGE
Step
3: RETIRE=55
Step
4: BIRTHYEAR = 2020-AGE
Step
5: REMAINYEARS= RETIRE-AGE
Step
6: YEAR_TO_RETIRE =2020+AGE
Step
7: display “your birth year is” BIRTHYEAR
Step
8: display “it remains to retire”
YEAR_TO_RETIRE
Step
9: display “you will retire” YEAR TO RETIRE
Step
10: Stop.
---------------------------------------*******----------------------------------------------
(B). The flow
chart illustrates how the problem is solved
start Enter age
stop Retire =55 Birth year= 2020 - age Remain years=retire - age Year to retire =2020
+ age Print Birth year Print Remain years Print Year to retire
QUESTION
TWO:
(A). The following algorithm compare username and password whether they
meet with stored information, if both are correct, then will display “correct “
otherwise one of them is incorrect (the LOGICAL AND) is used for logical
comparison
Step
1:
enter username and password say
USERNAME,PASSWORD
Step
2:
USERNAME = CIVE, PASSWORD = 2020
Step
3:
if USERNAME ==CIVE and PASSWORD==2020 then
Print correct details
Else
Print either username or password mismatch
End if
start
Enter username and password Username= CIVE Password = 2020 if username== CIVE and
password= =2020 Correct details incorrect details stop true false B. The flow chart illustrates how
the problem is solved
QUESTION
3
start
Enter marks If marks >=90 Print A If marks >=80 Print B If marks >=70 Print C If marks >=50 Print D Print F yes no yes no yes no yes else stop
---------------------------------------*******----------------------------------------------
(B). Algorithm
illustrates how the problem is solved
Step
1: start
Step
2: enter your marks say MARKS
Step
3: if MARKS >=90 then
Print A
else if MARKS >=80 then
Print B
else if MARKS >=70 then
Print C
else if MARKS >=50 then
Print D
Else
Print F
End if
Step
4: stop
QUESTION 4
(A). Create algorithm to display numbers form 0 to
100 using looping repetition (for loop)
Step
1:
start
Step
2:
character i=0
Step
3:
for i < = 100
Display i
The flow chart illustrates how the
problem is solved
Step
4: i++
Step
5: stop
start i=0 Print i i ++ For i < = 100 stop false true
QUESTION
5
(B). Create algorithm to display numbers form 0 to
100 using looping repetition (while
loop)
Step
1: start
Step
2: character i=0
Step 3:
do
print i
i++
Step
5: while
i<=100
Step
6: end loop
Step
7: stop
---------------------------------------*******----------------------------------------------
The flow chart illustrates how the problem is solved
start i=0 Read i Print i i++ i<=100 stop False True ?
RAM DIAGRAM VS
PHYSICAL MODEL
Simple program adding
integer numbers by function
#include <iostream>
using namespace std;
int addition(int namba1,int namba2)
{
int result=namba1+namba2;
return result;
}
int main()
{
int
value1,value2;
cin>>value1>>value2;
cout
<<addition (value1,value2);
return
0;
}
(A)
RAM DIAGRAM ILLUSTRATION
RESERVED |
RESERVED |
FREE |
FREE |
3 |
2 |
5 |
3 |
2 |
RESERVED |
RESERVED |
RESERVED |
FREE |
FREE |
FREE |
FREE |
FREE |
Value2 Value1 results Namba2 Value22 results Namba2 Namba1 Value1 Namba1
(B)
PHYSICAL MODEL
(i)
Memory
status before variable declaration:
(ii)
Variable
reservation
Value 2 Namba1 Namba2 result Value1
(iii)
Data
feeding
Value 2 Namba1 Namba2 result Value1
(iv)
Data
processing
Value 2 Namba1 Namba2 result Value1
No comments:
Post a Comment