Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

THE UNIVERSITY OF DODOMA

COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION

DEGREE PROGRAM: BSc. CSDFE 1

COURSE NAME: INTRODUCTION TO HIGH LEVEL PROGRAMMING

COURSE CODE: CP 123

NAME OF STUDENT: MUHAMED HASHIM SADIKI

REGISTRATION NO: T23-03-15752

INDIVIDUAL ASSIGNMENT 02.

Page 1 of 1
ASSIGNMENT ON FILES.

1. Example of output file code.

// Creating output files

#include <fstream>

#include <iostream>

using namespace std;

int main()

ofstream outfile;

outfile.open(“.muddy.txt”);

int m,u,d;

m = 10;

u = 2;

d = u*d;

outfile << m;

outfile << u;

outfile << d;

outfile.close();

return 0;

RAM DIAGRAMS.

FREE
FREE

Page 2 of 2
FREE
FREE
FREE
FREE
FREE

RESERVED
RESERVED
outfile
RESERVED

RESERVED
RESERVED
RESERVED

10
2
20
outfile

10
2
20

Page 3 of 3
2. Example of input file code.

// Creating input files

#include <fstream>

#include <iostream>

using namespace std;

int main()

ifstream infile;

infile.open(“muddy.txt”);

int a, b, c;

infile >> a;

infile >> b;

infile >> c;

infile.close();

cout << a;

cout << b;

cout << c;

return 0;

RAM DIAGRAMS.

FREE
FREE

Page 4 of 4
FREE
FREE
FREE
FREE
FREE

RESERVED
RESERVED
RESERVED
infile

RESERVED
RESERVED

a RESERVED

10
2

Page 5 of 5
infile 20

10
2
a 20

Page 6 of 6
3. Functions of files

// Creating output and input files

#include <fstream>

#include <iostream>

using namespace std;

void output()

ofstream outfile;

outfile.open(“muddy.txt”);

int m,u,d;

m= 10;

u = 2;

d = m*u;

outfile << m;

outfile << u;

Page 7 of 7
outfile << d;

outfile.close();

void input()

ifstream infile;

infile.open(“muddy.txt”);

int a, b, c;

infile >> a;

infile >> b;

infile >> c;

infile.close();

cout << a;

cout << b;

cout << c;

int main()

void output();

void input();

output();

input();

return 0;

Page 8 of 8
}

4. Array content outputted to output file.

#include <fstream>

#include <iostream>

using namespace std;

int main()

int array [3][1] = {{1}, {2}, {3}};

ofstream outputfile;

outputfile.open(“Output.txt”);

if (outputfile.is_open()){

for(int i = 0; i < 3; i++){

for(int j = 0; j < 1; j++ ){

outputfile << array[i][j] << “ “;

outputfile << endl;

outputfile.close();

cout << “Array content written to output.txt successfully” << endl;

} else {

cout << “Unable to open output.txt file” << endl;

Page 9 of 9
return 0;

5. Output the results to the screen.

#include <fstream>

#include <iostream>

using namespace std;

int main()

ifstream inputfile;

inputfile.open(“Output.txt”);

if (inputfile.is_open()){

int array[3][1];

for(int i = 0; i < 3; i++){

for(int j = 0; j < 1; j++){

inputfile >> array[i][j];

cout << “Array after multiplying each element by 4:” << endl;

for(int i = 0; i < 3; i++){


for(int j = 0; j < 1; j++){
array[i][j] *= 4;

cout << array[i][j] << “ “;

Page 10 of 10
}

cout << endl;

inputfile.close();

} else {

cout << “Unable to open output.txt file” << endl;

return 0;

ASSIGNMENT ON POINTRES.

1. First worked example.

//Pointers variables

#include <iostream>

using namespace std;

int main()

int i = 15;

int *p1;

int *p2;

p1 = &i;

p2 = p1;

cout << *p1 << endl;

cout << *p2 << endl;

Page 11 of 11
}

RAM DIAGRAMS.

FREE
FREE
FREE
FREE
FREE

i RESERVED x01
RESERVED
p1 x02
RESERVED
p2 x03

I 15 x01
x01
p1 x02
x02
p2 x03

2. Second worked example.

//Pointers variables

#include <iostream>

Page 12 of 12
using namespace std;

int main()

int i = 15;

int j = 30;

int *p1;

int *p2;

p1 = &i;

p2 = &j;

cout << *p1 << endl;

cout << *p2 << endl;

RAM DIAGRAMS.

FREE
FREE
FREE
FREE
FREE

RESERVED
RESERVED

Page 13 of 13
i RESERVED x01
RESERVED
j x02

p1 x03

p2 x04

15
i x01
30
j x02
x01
p1 x02 x03

p2 x04

Page 14 of 14
The name of the book used is “C++ Primer" by Stanley B. Lippman, Josée Lajoie, and
Barbara E. Moo pdf download”

Also C++ by w3schools

Page 15 of 15

You might also like