u

Friday, 14 September 2012

C++ Program to assign string in a double dimensional array and display the contents

C++ Program to assign string in a double dimensional array and display the contents


 #include <iostream.h>
#include <conio.h>


enum {NAMESIZE=20};

// Print out the first n names of a
void printNames(char a[][NAMESIZE], int n)
{
    for (int row=0; row<n; row++)
    {
        for (int clmn=0; a[row][clmn] != '\0'; clmn++)
        {
            cout << a[row][clmn];
        }
        cout << endl;
    }
}

// Print out the first n names of a
void printNames(char *a[], int n)
{
    for (int k=0; k<n; k++)
    {
        cout << a[k] << endl;
    }
}

void main(void)
{
    char names1[][NAMESIZE] = {"ankur", "rose", "simran", "gauri"};
    char *names2[] = {"lallan", "ballan", "sonal", "monika"};

    cout << "The size of names1 is " << sizeof(names1) << endl;
    cout << "The size of names2 is " << sizeof(names2) << endl;
    getch();


    cout << endl << "Printing out: char names1[][NAMESIZE]" << endl;
    printNames(names1, 4);

    cout << endl << "Printing out: char *names[]" << endl;
    printNames(names2, 4);
    getch();

    cout << endl << "Printing out: names1[row]" << endl;
    for (int row=0; row < 4; row++)
    {
        cout << names1[row] << endl;
    }

    cout << endl << "Printing out: names2[row]" << endl;
    for (row=0; row < 4; row++)
    {
        cout << names2[row] << endl;
    }

    getch();clrscr();
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More