Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A function read that reads an array of characters from a user specified size (e.

ID: 3581694 • Letter: A

Question

A function read that reads an array of characters from a user specified size (e.g., 21). Please note that size of the array should be an input to your program. A function init that creates an array of characters b from an array of characters a with the following rule: if ASCII code of a[i] is divisible by 5 then b[i] = a[i], else b[i] is set equal to the letter j. A function print..table that prints the array a and array b (see 1. and 2.). Sample Output: (For an input array of size 5 and input a n d e f)a: j n: n d: d e: j f: j Paste your C++ code for this problem here Show test results of program running on two test cases and corresponding screenshots here: Paste screen shots from your compiler for each test case here.

Explanation / Answer

#include<iostream>

using namespace std;

void read(char *a, unsigned int arr_size)
{
cout<<"Enter character one by one " << endl;
for(size_t i = 0; i < arr_size; i++)
{
cin>>a[i];
}
}
void init(char *a, char *b, unsigned int arr_size)
{
for(size_t i = 0; i < arr_size; i++)
{
if(int(a[i]) % 5 == 0) //ascii_value % 5 = 0 shows divisibility by 5
b[i] = a[i];

else
b[i] = 'j';
}
}
void print_table(char *a, char *b, unsigned int arr_size)
{
cout<<endl;
for(size_t i =0; i < arr_size; i++)
{
cout<<a[i]<<" : "<<b[i]<<endl;
}
}
int main()
{
unsigned int arr_size;

cout<<"Enter the array size : ";
cin>>arr_size;

char *a = new char[arr_size]; //creating dynamic array of size entered by user
char *b = new char[arr_size];
read(a,arr_size); //reading character from user
init(a,b,arr_size);
print_table(a,b,arr_size); //printing array a and b
return 0;

}

Please refer below output for your reference

Enter the array size : 5
Enter character one by one
a
n
d
e
f

a : j
n : n
d : d
e : j
f : j

Process returned 0 (0x0) execution time : 5.987 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote