Okay, I have another problem that I can't figure out and it has to do with the I/O file stuff.

This is the problem:

Write a function, CountElement, that receives two arguments: a 2-dimensional char array
and the size of the array. This function counts the number of digits, small letters, capital letters and
symbols in the char array, and returns the counts. Write a driver function to test your function and
print your results to the computer screen and to a text file.

This is my attempt but I am not even sure if I am doing this correctly:

#include <iostream>//include statement(s)
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;//using namespace statement(s)

const int i = 1000;

void createArray(int a[][1000]);
void CountElement(char a[][1000]);
void printResults(char c[][1000]);

int main()
{
    int a[i][i] = {0};

    createArray(a);

    system ("PAUSE");//black box to appear
    return 0;//return statment(s)
}

void createArray(char a[][1000])
{
    int counter = 0;
    for (int i = 0; i < 1000; i++)
    {
        for (int j = 0; j < 1000; j++)
        {
            a[i][j] = counter++;//initalize the element for the array
        }
    }

    return;
}

void CountElement(char a[][1000])
{

    return;
}

void printResults(char c[][1000])
{

    return;
}

I really need help with this one because I have no idea what I am doing with this code. Thanks a bunch!

Recommended Answers

All 5 Replies

Not sure what you are trying to do ?

function counts the number of digits, small letters, capital letters and symbols in the char array, and returns the counts

Are you to read a text file into an array of words?

Or ... to read the whole file into one big char array?

Or ... are you just given some array of char to start?
(or an array of arrays of char ... perhaps a 'ragged array' ?)

Yeah, I'm confused on what the problem is asking as well. I'll email the instructor and ask. His problems are so vague lol XD

Hey David, this is what I have but I want to make changes on it though, the code works but I want it to look different though. Please nothing too fancy.

/* Take Home Assignment 6: Problem 2
   By: Renee Yaldoo
   access ID: ez7086
   Date: 6/18/2014
   Time Modified: 8:18 PM

   2. (40 points) Write a function, CountElement, that receives two arguments: a 2-dimensional char array 
       and the size of the array. This function counts the number of digits, small letters, capital letters and 
       symbols in the char array, and returns the counts. Write a driver function to test your function and 
       print your results to the computer screen and to a text file. 
*/

#include<iostream>//include statement(s)
#include<iomanip>
#include<fstream>

using namespace std;//using namespace statement(s)

const int ind1Siz = 10;//constant integer array size(s)
const int ind2Siz = 10;

void countElement(char arry[][ind2Siz], int& ArrySiz);//void function header to count elements
void showArry(char arry[][ind2Siz]);//void function header to show the array
void outputResults(int a, int b, int c, int d, int e);//void function header to output file

int main()
{
    char charArr[ind1Siz][ind2Siz] = {{'y', '7', '9', '0', 'K', '!'}, {'A', ',', '1', 'o', 'U', 'P'}, {'W', 'g', '6', '2', '%', '$'}};//initializing array
    int actualSIZE;//variable declaration(s)

    showArry(charArr);//show array function call
    countElement(charArr, actualSIZE);//countElement function call

    system ("PAUSE");//black box to appear
    return 0;//return statement(s)
}

void countElement(char arry[][ind2Siz], int& ArrySiz)
{
    ArrySiz = 0;//variable declaration(s) for counter(s)
    int numberCounter = 0;
    int lowerCaseCounter = 0;
    int upperCaseCounter = 0;
    int symbolCounter = 0;
    int ASCIIcode;

    for(int a = 0; a < ind1Siz; a++)//create and initalize elements
    {
        for(int b = 0; b < ind2Siz; b++)
        {
            ASCIIcode = static_cast<int>(arry[a][b]);//converts ASCIIcode into integers from array

            if(ASCIIcode >= 48 && ASCIIcode <= 57) 
                numberCounter++; //number character, if the ASCIIcode is in this range; adds a counter for number character

            if(ASCIIcode >= 97 && ASCIIcode <= 122) 
                lowerCaseCounter++; //lower case letter, if the ASCIIcode is in this range; adds a counter for lower case letter

            if(ASCIIcode >= 65 && ASCIIcode <= 90)  
                upperCaseCounter++; //upper case letter, if the ASCIIcode is in this range; adds a counter for upper case letter

            if((ASCIIcode >= 33 && ASCIIcode <= 47) || (ASCIIcode >= 58 && ASCIIcode <= 64) || (ASCIIcode >= 91 && ASCIIcode <= 96) || (ASCIIcode >= 126 && ASCIIcode <= 129)) 
                symbolCounter++; // symbol character, if the ASCIIcode is in these 4 ranges; adds a counter for symbol

            if(ASCIIcode != 0) 
                ArrySiz++; //element, if the char array element is not null character; adds a counter for arraysize
        }
    }

    cout << "The array has " << numberCounter << " digits." << endl;
    cout << "The array has " << lowerCaseCounter << " lower cased letters." << endl;
    cout << "The array has " << upperCaseCounter << " upper cased letters." << endl;
    cout << "The array has " << symbolCounter << " symbols." << endl;
    cout << "The array has " << ArrySiz << " out of " << ind1Siz * ind2Siz << " elements in it." << endl;

    outputResults(ArrySiz, numberCounter, lowerCaseCounter, upperCaseCounter, symbolCounter);

    return;
}

void showArry(char arry[][ind2Siz])
{
    cout << "The 2-dimensional array contains the following: " << endl;
    for (int a = 0; a < ind1Siz; a++)//creates first index array
    {
        for (int b = 0; b < ind2Siz; b++)//creates second index array
        {
                cout << setw(3) << arry[a][b];//to space out the index
                if ((b + 1) % ind2Siz == 0)//to make a square array when (b + 1) % ind2Siz equals 0
                cout << endl;
        }
    }

    cout << endl;

    return;
}

void outputResults(int ArrySiz, int numberCounter, int lowerCaseCounter, int upperCaseCounter, int symbolCounter)
{
    ofstream output;//creating output text
    output.open("outputResult.txt");

    if(!output.fail())//if output doesn't fail...
        cout << "An output file called 'outputResult.txt' has been created." << endl;
    else//if output fails...
        cout << "An output file could not be created!" << endl;

    output << "The array has " << numberCounter << " digits." << endl;
    output << "The array has " << lowerCaseCounter << " lower cased letters." << endl;
    output << "The array has " << upperCaseCounter << " upper cased letters." << endl;
    output << "The array has " << symbolCounter << " symbols." << endl;
    output << "The array has " << ArrySiz << " out of " << ind1Siz * ind2Siz << " elements in it." << endl;

    output.close();

    cout << endl;
    return;
}

Take a look at this revision that uses a (data) struct to simplify and help organize the flow of the data processing:

/*
    Write a function, CountElement, that receives two arguments:
    * a 2-dimensional char array
    * and the size of the array.
    This function counts
        1. the number of digits,
        2. small letters,
        3. capital letters and
        4. symbols in the char array,
    and returns the counts.

    Write a driver function to test your function and
    print your results
        to the computer screen
        and to a text file.

    Note: in this example, both of the above two
    functions are combined into just one function.

*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype> // re. isalpha, islower, etc...

using namespace std;

// constant char array size (of each row) //
const int SIZE2 = 6;

const char* FNAME = "outputResult.txt";

struct MyData
{
    int numLowerCase;
    int numUpperCase;
    int numDigits;
    int numOther;

    MyData() : numLowerCase(0), numUpperCase(0),
               numDigits(0), numOther(0) {}
} ;


// header of void function to analyse, show and file the array //
void calShowAndFile( const char ary[][SIZE2], int size );



int main()
{
    // initial/get a demo test array ... //
    const char ary[][SIZE2] =
    {
        { 'y', '7', '9', '0', 'K', '!' },
        { 'A', ',', '1', 'o', 'U', 'P' },
        { 'W', 'g', '6', '2', '%', '$' },
        { '*', 'h', 'J', '#', '@', 'i' }
    };

    int size = sizeof ary / sizeof *ary; // number of rows //

    calShowAndFile( ary, size );

    //system ("PAUSE");// NOT portable ... //
    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();
    return 0;
}



void calShowAndFile( const char ary[][SIZE2], int size )
{
    ofstream fout( FNAME ); // Note: FNAME is a global const //
    if( fout )
    {
        fout << "The 2-dimensional array contains the following:\n";
        cout << "The 2-dimensional array contains the following:\n";

        MyData data; // default ctor... initials all to zero //
        for( int a = 0; a < size; ++a )
        {
            for( int b = 0; b < SIZE2; ++b )
            {
                fout << setw(3) << ary[a][b];
                cout << setw(3) << ary[a][b];
                if( isalpha( ary[a][b] ))
                {
                    if( islower( ary[a][b] )) ++ data.numLowerCase;
                    else ++ data.numUpperCase;
                }
                else if( isdigit( ary[a][b] )) ++ data.numDigits;
                else ++ data.numOther;
            }
            fout << endl;
            cout << endl;
        }

        fout << "\nNumber of lower case letters : "
             << data.numLowerCase
             << "\nNumber of upper case letters : "
             << data.numUpperCase
             << "\nNumber of digits             : "
             << data.numDigits
             << "\nNumber of other symbols      : "
             << data.numOther << endl;
        cout << "\nNumber of lower case letters : "
             << data.numLowerCase
             << "\nNumber of upper case letters : "
             << data.numUpperCase
             << "\nNumber of digits             : "
             << data.numDigits
             << "\nNumber of other symbols      : "
             << data.numOther << endl;
    }
    else
        cout << "\nThere was a problem opening file "
             << FNAME << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.