Beginner in a c++ course, and deeply confused with how this program should go.

Your program uses a function fillArray to get no more than 20 characters from the user and store them in array char a[20], and then display on screen those characters stored in array a by calling function displayArray, and then your program uses another function toUpperCase that stores all those characters in array a into array b, such that

• all upper case letters in array a will be stored in array b,

• and the upper case versions of all lower case letters in array a will be stored in array b

• and the order of all letters in array b will be the same as that of array a.

and then display on screen the characters in array b by calling function displayArray.

Use a named constant ARRAY_SIZE to specify the maximum number of allowed characters in both array a and array b. That is, in your main() function, you write the following statement
const int ARRAY_SIZE=20;

Your function definitions of those three functions should follow the declarations given below.

void fillArray(char a[], int size, int& num_used); /*
This function will ask the user to enter no more than size letters, and the function will store those letters in array a.

A user can choose to enter size letters or if the user wants to enter less than size letters, the user can indicate that he/she has finished entering letters by entering character ;

Note that character ; will not be stored in array a.

Pre-condition: the declared size of array a is given by parameter size.

The argument variable passed to the call-by-reference parameter num_used should exist and should be an int variable in the code that calls this function.

Post-condition: array a will contain a number of letters entered by the user. The number is given stored in num_used parameter.

num_used should be less than or equal to size.

For example,

Suppose in a main() function, we have the following piece of code
char b[ARRAY_SIZE]; //ARRY_SIZE is a named constant for value 20 int num=0;
fillArray(b,ARRAY_SI ZE, num);
cout << num<<endl;

If the user enters A

b c D

Then, the above function call (i.e., fillArray(b, ARRAY_SIZE, num);)

will stored AbcD in array b. And the above “cout<<num<<endl;” will display 4.
*/
void displayArray(char a[], int num_used); /*
This function will display on screen all letters in array a.

Precondition: array a should exist. num_used should be a valid integer in the

sense that it represents the actual number of letters in array a. Note that num_used could be equal to the declared size of array a or could be less than the declared size of a.

Postcondition: Those num_used letters in array a are displayed on screen in a single line in the order that they are stored in array a.

For example,

Suppose array b contains AbcD, and variable num has value 4 (which is the actual number of letters stored in array b).
Then, the following function call displayArray(b, num);
will displayed on screen AbcD

*/

void toUpperCase(const char source_array[], char dest_array[], int num_used ); /*

This function will store a copy of all letters in source_array into dest_array such that all lower case letters in source_array are changed to upper case letters and those originally upper case letters will remain unchanged in dest_array.
The order of all letters in source_array will remain unchanged in dest_array.

Pre-condition: source_array[] contains num_used letters.

dest_array[]’s declared size should be greater than or equal to num_used.

Post-condition: dest_array contains all those letters in source_array, and

all upper case letters in source_array will be stored in dest_array, and the upper case versions of all lower case letters in source_array will be stored in dest_array, and the order of all letters in source_array will be the same as that of dest_array.

For example,

Suppose array a contains AbcD, and num_used is 4 Then after the following function call toUpperCase(a, b, 4)
array b will contain ABCD.

Note: use toupper() library function to convert to upper.
*/

Please use the following main() function for this lab assignment.

int main()

{

const int ARRAY_SIZE=20; char a[ARRAY_SIZE];
char b[ARRAY_SIZE];

int num=0;

fillArray(a, ARRAY_SIZE, num);

cout<<"Now display the array that has just been entered:\n"; displayArray(a, num);

toUpperCase(a, b, num);

cout<<"Now display the new array after calling toUpperCase function:\n"; displayArray(b, num);
}
An example execution of the program is displayed below, where the underlined text is entered by the user.

./a.out

Enter up to 20 characters.

Stop entering characters by entering ';' (which is NOT included in the array).

abbCCCdeFFxxYY;
Now display the array that has just been entered:
abbCCCdeFFxxYY

Now display the new array after calling toUpperCase function:

ABBCCCDEFFXXYY



Add a function called deleteRepeats, that will only keep one instance of a letter if it is repeated multiple times in the array that is entered by the user. Note that letters are case sensitive. For example, a and A are regarded as different letters.

For example, if the user enters array abbCCCdeYY

then your program will use another array to store only abCdeY

Your function definition of deleteRepeats should use the following declaration.

void deleteRepeats(const char source_array[], char dest_array[], int num_used, int& num_no_repeats );

/*

Pre-condition: source_array[] contains num_used characters. The declared size of dest_array should be no less than num_used

Post-condition: dest_array[] contains only distinct letters in source_array[], and the order of those characters in dest_array[] is the same as that of source_array[].

Call-by-reference parameter num_no_repeats stores the actual number of letters in dest_array[].
*/

To test your function, use the following main program.

int main()
{

const int ARRAY_SIZE=20;

char a[ARRAY_SIZE]; char b[ARRAY_SIZE]; char c[ARRAY_SIZE];

int num=0;

int num_no_repeats=0;

fillArray(a, ARRAY_SIZE, num);

cout<<" Now display the array that has just been entered:\n"; displayArray(a, num);

changeCase(a, b, num);

cout<<"Now display the new array after calling toUpperCase function:\n"; displayArray(b, num);

deleteRepeats(a, c, num, num_no_repeats);

cout<<"Now display the new array after calling deleteRepeats function:\n"; displayArray(c, num_no_repeats);
}
closed account (48T7M4Gy)
So which part are you having trouble on?
with the upper case and delete repeats part. I don't know how the code would look.
closed account (48T7M4Gy)
Well, you have 20 characters. To convert a character char x to uppercase all you do is write
char new_char = toupper(x); Now it's your turn to write the stat of the assignment, #include's , main, return all that stuff plus the bit on uppercase. Maybe include a test array of 20 characters. Leave the duplicates for a while.t
Okay, so I've written out my code, but it's not displaying any of the characters that I enter in when I am executing the program. Would you be able to point out why the output is not correct?

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <ctype.h>
using namespace std;
const int array_size=20;


void fillArray(char a[], int size, int& num_used);
//Pre-condition: the declared size of array a is given by parameter size.
//Post-condition: array a will contain a number of letters entered by the user.

void displayArray(char a[], int num_used);
//Pre-condition: array a should exist. num_used should be a valid integer in the sense that it represents the actual number of letters in array a. Note that num_used could be equal to the declared size of array a or could be less than the declared size of a.
//Postcondition: Those num_used letters in array a are displayed on screen in a single line in the order that they are stored in array a.

void toUpperCase(const char source_array[], char dest_array[], int num_used );
//Pre-condition: source_array[] contains num_used letters. dest_array[]’s declared size should be greater than or equal to num_used.
//Post-condition: dest_array contains all those letters in source_array, and all upper case letters in source_array will be stored in dest_array, and the upper case versions of all lower case letters in source_array will be stored in dest_array, and the order of all letters in source_array will be the same as that of dest_array.

void deleteRepeats(const char source_array[], char dest_array[], int num_used, int& num_no_repeats);
//Pre-condition: source_array[] contains num_used characters. The declared size of dest_array should be no less than num_used
//Post-condition: dest_array[] contains only distinct letters in source_array[], and the order of those characters in dest_array[] is the same as that of source_array[]. 

int main()

{

const int ARRAY_SIZE=20; char a[ARRAY_SIZE];
char b[ARRAY_SIZE];
char c[ARRAY_SIZE];

int num=0;

int num_no_repeats=0;

fillArray(a, ARRAY_SIZE, num);

cout<<"Now display the array that has just been entered:\n"; displayArray(a, num);

toUpperCase(a, b, num);

cout<<"Now display the new array after calling toUpperCase function:\n"; displayArray(b, num);

deleteRepeats(a, c, num, num_no_repeats);

cout<<"Now display the new array after calling deleteRepeats function:\n"; displayArray(c, num_no_repeats);
} 
void fillArray(char a[], int size, int& num_used)
{

cout << "Enter up to 20 chracters: " << endl;
cout << "Stop entering characters by entering ';' " << endl;
cin>> size >>  num_used;

int index=0;
char next;
cin >>next;
 while ((next !=';') && (index <size))
 {
        a[index] =next;
        index++;
        cin >>next;
 }
    num_used = index;
}

 void displayArray(char a[], int num_used)
{

   for ( int loop =0;loop <= num_used;loop ++)
  cout << a[loop];

 cout << num_used << endl;


}

void toUpperCase(const char source_array[], char dest_array[], int num_used)
{
int i =0;
    for ( i=0; i < num_used; i++) {
        dest_array[i] = toupper(source_array[i]);
        }
    cout << dest_array[i];
}


void deleteRepeats(const char source_array[], char dest_array[], int num_used, int& num_no_repeats)
{
        int outputPos=0;
for (int i = 0; i < num_used; i++) {
 char current = source_array[ i ];
    if ( current != dest_array[outputPos] ) {
                 dest_array[outputPos] = current;
        outputPos++; }
                                    }

}

Last edited on
closed account (48T7M4Gy)
OK I'll have a look. While you're waiting you might like to use code tags because then other people might decide it's easier to read and comment on too - all to your advantage.

For code tags go into edit on your post, select your coding and press the <> button in he toolbar on the right. :)
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
void fillArray(char a[], int size, int& num_used)
{
    
    cout << "Enter up to 20 chracters: " << endl;
    cout << "Stop entering characters by entering ';' " << endl;
    //cin>> size >> num_used; // <-- WRONG :) 



Delete the line I've indicated. Can you see why?
Oh! Is it because I already have the section for input in the following lines down where I have "cin >> next"?
closed account (48T7M4Gy)
That's the one.

What you might also do there is modify displayArray():

1
2
3
4
5
6
7
8
9
void displayArray(char a[], int num_used)
{
    
    for ( int loop =0;loop <= num_used;loop ++)
        cout << a[loop];
    
    cout << "\nNum used: " << num_used << endl; //<---
    
}


That way the number that was otherwise just appearing out of nowhere has some meaning in the output. It looked like an error in the output showing up until I worked out why it was there.

Thanks for the code tags.

Looks like you're in business - better than the previous confusion.
Last edited on
closed account (48T7M4Gy)
Another way with the while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void fillArray(char a[], int size, int& num_used)
{
    
    cout << "Enter up to 20 chracters: " << endl;
    cout << "Stop entering characters by entering ';' " << endl;
    
    num_used = 0;
    char next = ' ';
    
    while ( cin >> next and next != ';')
    {
        a[num_used] = next;
        num_used++;
        if(num_used > size)
        {
            cout << "Limit exceeded\n";
            break;
        }
    }
}
Last edited on
Ah ! I got it! Thanks so much for the help :)

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <ctype.h>
using namespace std;
const int array_size=20;


void fillArray(char a[], int size, int& num_used);
//Pre-condition: the declared size of array a is given by parameter size.
 //Post-condition: array a will contain a number of letters entered by the user.

void displayArray(char a[], int num_used);
//Pre-condition: array a should exist. num_used should be a valid integer in the sense that it represents the actual number of letters in array a. Note that num_used could be equal to the declared size of array a or could be less than the declared size of a.
//Postcondition: Those num_used letters in array a are displayed on screen in a single line in the order that they are stored in array a.

void toUpperCase(const char source_array[], char dest_array[], int num_used );
//Pre-condition: source_array[] contains num_used letters. dest_array[]’s declared size should be greater than or equal to num_used.
//Post-condition: dest_array contains all those letters in source_array, and all upper case letters in source_array will be stored in dest_array, and the upper case versions of all lower case letters in source_array will be stored in dest_array, and the order of all letters in source_array will be the same as that of dest_array.

void deleteRepeats(const char source_array[], char dest_array[], int num_used, int& num_no_repeats);
//Pre-condition: source_array[] contains num_used characters. The declared size of dest_array should be no less than num_used
//Post-condition: dest_array[] contains only distinct letters in source_array[], and the order of those characters in dest_array[] is the same as that of source_array[]. 

int main()

{

const int ARRAY_SIZE=20;
char a[ARRAY_SIZE];
char b[ARRAY_SIZE];
char c[ARRAY_SIZE];

int num=0;

int num_no_repeats=0;

fillArray(a, ARRAY_SIZE, num);

cout<<"Now display the array that has just been entered:\n"; displayArray(a, num);

toUpperCase(a, b, num);

cout<<"Now display the new array after calling toUpperCase function:\n"; displayArray(b, num);

deleteRepeats(a, c, num, num_no_repeats);

cout<<"Now display the new array after calling deleteRepeats function:\n"; displayArray(c, num_no_repeats);

return 0;
}

void fillArray(char a[], int size, int &num_used) {

    cout << "Enter up to " << size << " characters using a ';' to end the input" << endl;
    num_used = 0;
do {
        char input;
        cin >> input;
        if (input != ';') {
            a[num_used] = input;
            num_used++;
        } else {
            break;
        }
    } while (num_used < size);
}


void displayArray(char a[], int num_used) {

    for (int i = 0; i < num_used; i++) {
        cout << a[i];
    }

    cout << endl;
}
void toUpperCase(const char source_array[], char dest_array[], int num_used)
{
int i =0;
    for ( i=0; i < num_used; i++) {
        dest_array[i] = toupper(source_array[i]);
        }

}


void deleteRepeats(const char source_array[], char dest_array[], int num_used, int& num_no_repeats)
{
   num_no_repeats = 1;
    dest_array[0] = source_array[0];
     for (int i = 1; i < num_used; i++) {
      char current = source_array[i];
      if ( current != dest_array[num_no_repeats-1] ) {
                 dest_array[num_no_repeats] = current;
        num_no_repeats++; }
 }
Topic archived. No new replies allowed.