0% found this document useful (0 votes)
5 views13 pages

David Vincent Assignment

The document is a C++ program that prints ASCII values for characters in given strings and calculates their ASCII sums. It lists all ASCII characters from 0 to 127, and computes the ASCII sums for names and additional information like matric number, department, and status. The program outputs the ASCII values and their corresponding sums for each input string.

Uploaded by

dreysaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

David Vincent Assignment

The document is a C++ program that prints ASCII values for characters in given strings and calculates their ASCII sums. It lists all ASCII characters from 0 to 127, and computes the ASCII sums for names and additional information like matric number, department, and status. The program outputs the ASCII values and their corresponding sums for each input string.

Uploaded by

dreysaiah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

assignment

#include <iostream>

#include <string>

#include <iomanip>

// Function to print each character with its ASCII value and calculate the ASCII sum

int printAsciiValues(const std::string& text) {

int sum = 0;

std::cout << "\nASCII values for \"" << text << "\":\n";

for (char ch : text) {

int asciiValue = static_cast<int>(ch);

std::cout << ch << " : " << asciiValue << std::endl;

sum += asciiValue;

return sum;

// Function to list all ASCII characters (0-127)

void listAsciiCharacters() {

std::cout << "ASCII Table:\n";

std::cout << "Char | ASCII Value\n";

std::cout << "-------------------\n";

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

std::cout << std::setw(4) << static_cast<char>(i) << " | " << i << std::endl;
}

int main() {

// Task 1: List all ASCII characters

listAsciiCharacters();

// Task 2: Print ASCII values and calculate sum for each name part

std::string surname = "David";

std::string middleName = "Unekwuojo";

std::string firstName = "Vincent";

std::cout << "\nASCII value sum of names:\n";

int surnameSum = printAsciiValues(surname);

int middleNameSum = printAsciiValues(middleName);

int firstNameSum = printAsciiValues(firstName);

std::cout << "Total ASCII sum of Surname (" << surname << "): " << surnameSum << std::endl;

std::cout << "Total ASCII sum of Middle Name (" << middleName << "): " << middleNameSum <<
std::endl;

std::cout << "Total ASCII sum of First Name (" << firstName << "): " << firstNameSum << std::endl;

// Task 3: Print ASCII values and calculate sum for matric number, department, and status

std::string matricNumber = "2312108"; // Replace with actual matric number

std::string department = "Computer Science";

std::string status = "200 level";

std::cout << "\nASCII value sum of additional information:\n";

int matricSum = printAsciiValues(matricNumber);


int departmentSum = printAsciiValues(department);

int statusSum = printAsciiValues(status);

std::cout << "Total ASCII sum of Matric Number (" << matricNumber << "): " << matricSum <<
std::endl;

std::cout << "Total ASCII sum of Department (" << department << "): " << departmentSum <<
std::endl;

std::cout << "Total ASCII sum of Status (" << status << "): " << statusSum << std::endl;

return 0;

OUTPUT

ASCII Table:

Char | ASCII Value

-------------------

|0

|1

|2

|3

|4

|5

|6

|7

|8
|9

| 10

| 11
| 12

| 13
| 14

| 15

| 16

| 17

| 18

| 19

| 20

| 21

| 22

| 23

| 24

| 25

| 26

| 27

| 28

| 29

| 30

¬ | 31

| 32

! | 33

" | 34

# | 35

$ | 36

% | 37

& | 38
' | 39

( | 40

) | 41

* | 42

+ | 43

, | 44

- | 45

. | 46

/ | 47

0 | 48

1 | 49

2 | 50

3 | 51

4 | 52

5 | 53

6 | 54

7 | 55

8 | 56

9 | 57

: | 58

; | 59

< | 60

= | 61

> | 62

? | 63
@ | 64

A | 65

B | 66

C | 67

D | 68

E | 69

F | 70

G | 71

H | 72

I | 73

J | 74

K | 75

L | 76

M | 77

N | 78

O | 79

P | 80

Q | 81

R | 82

S | 83

T | 84

U | 85

V | 86

W | 87

X | 88
Y | 89

Z | 90

[ | 91

\ | 92

] | 93

^ | 94

_ | 95

` | 96

a | 97

b | 98

c | 99

d | 100

e | 101

f | 102

g | 103

h | 104

i | 105

j | 106

k | 107

l | 108

m | 109

n | 110

o | 111

p | 112

q | 113
r | 114

s | 115

t | 116

u | 117

v | 118

w | 119

x | 120

y | 121

z | 122

{ | 123

| | 124

} | 125

~ | 126

| 127

ASCII value sum of names:

ASCII values for "David":

D : 68

a : 97

v : 118

i : 105

d : 100

ASCII values for "Unekwuojo":


U : 85

n : 110

e : 101

k : 107

w : 119

u : 117

o : 111

j : 106

o : 111

ASCII values for "Vincent":

V : 86

i : 105

n : 110

c : 99

e : 101

n : 110

t : 116

Total ASCII sum of Surname (David): 488

Total ASCII sum of Middle Name (Unekwuojo): 967

Total ASCII sum of First Name (Vincent): 727

ASCII value sum of additional information:

ASCII values for "2312108":


2 : 50

3 : 51

1 : 49

2 : 50

1 : 49

0 : 48

8 : 56

ASCII values for "Computer Science":

C : 67

o : 111

m : 109

p : 112

u : 117

t : 116

e : 101

r : 114

: 32

S : 83

c : 99

i : 105

e : 101

n : 110

c : 99

e : 101
ASCII values for "200 level":

2 : 50

0 : 48

0 : 48

: 32

l : 108

e : 101

v : 118

e : 101

l : 108

Total ASCII sum of Matric Number (2312108): 353

Total ASCII sum of Department (Computer Science): 1577

Total ASCII sum of Status (200 level): 714

You might also like