Question

I have just started on c++ and have encountered Expression:

(unsigned)(c+1) <=256 Error

when I am trying to run the program. The program is suppose to generate an array of letters (lowercase and uppercase) of size from 15 to 25. After the generation, using ONLY one while-loop, try to move all lowercase letters to the left of the array and all the uppercase letters to the right of the array. It have to contain the following criterias

(a) A function to construct the array
(b) A function to print out the array elements
(c) A function to swap two elements
(d) An iterative function (using ONE loop) to move the array elements so that all the lowercase letters are on the
left hand side of the array and all the uppercase letters are on the right hand side of the array.

Can anyone please help me with this as I am totally lost and thanks in advance!

#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>

using namespace std;

const int MAX = 100;
void consArray (char [], int);
void printOut (char [], int);
void swapEle (char[], int, int, int);
void iterative (char[], int); 

int main ()
{
    int length;
    char alpha[MAX];

    srand (time (NULL));

    cout << "Given the following array" << endl;
    length = rand() % 25 + 15;
    consArray(alpha, length);
    printOut(alpha, length);
    cout << "\nIterative swap of array" << endl;
    iterative(alpha, length);
    printOut(alpha, length);
}

void consArray (char myAlpha [], int length)
{
    int even, val;
    char alpha;

    for (int i=0; i < length; i++)
    {
        even =  rand() % 2;
        val =  rand() % 26;

        if (even == 0)
        {
            alpha = toupper(val + 65);
        }

        else 
        {
            alpha = tolower(val + 65);
        }
        myAlpha[i] = alpha;
    }

}

void printOut (char alpha[], int length)
{
    for (int i = 0; i < length; i++)
    {
        cout << "  " << alpha[i];
    }
    cout << endl;
}

void swapEle (char alpha[], int loca1, int loca2)
{
    char temp;
    temp = alpha[loca1];
    alpha [loca1] = alpha [loca2];
    alpha [loca2] = temp; 
}

void iterative (char alpha[], int length)
{
    int a = length;
    int i = 0; 

    while (i < length && a >= 0)
    {

        if(isupper(alpha[i]) && islower(alpha[a]))
        {
            swapEle (alpha, i, a);
            a--;
            i++;
        }
        else if (islower(alpha[i]) && isupper(alpha[a]))
        {
            i++;
            a--;
        }
        else if (islower(alpha[i]) && islower(alpha[a]))
        {
            i++;
        }
        else if (isupper(alpha[i]) && isupper(alpha[a]))
        {
            a--;
        }

    }
}
Was it helpful?

Solution

Your question is lacking some significant details, so this answer is more of an educated guess...

You seem to have two problems at hand (only the first one is the cause of your error):

  1. In function iterative, change int a = length to int a = length-1, because you are reading a "junk" character from alpha[a] upon the first iteration.

  2. In addition, you should probably change while (i < length && a >= 0) to while (i < a), because once i >= a, you have completed your work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top