11 Replies - 3395 Views - Last Post: 09 December 2012 - 05:55 AM Rate Topic: -----

#1 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Ragged Array and Pointers.

Posted 03 December 2012 - 10:54 PM

1. Prompt for user ID, station, and lab.
2. Fixed array[SIZE]; SIZE = 4.
3. Each array points to dynamic array

So do i have the user determine the array size with the pointer? I'm so confused how to do a ragged array. I get
that its a multidimensional array with multiple inputs and outputs. But how do I go about prompting the user for it
and printing out each possible outcome? Like user says station 1, lab 2, and their custom id number. I hate being
confused.

#include <iostream>

using namespace std;

int main ()
{
  int *lab1, *id, *station;
  cout << "What station are you going to be on? ";
  lab1 = int[i];
  cin >> i;
  if (lab1[0] == 0)
  {
      cout << "Error";
  }
  else
  {
	for (lab=0; n<i; n++)
	{
	  cout << "Enter ID number: ";
	  cin >> lab[n];
	}
	cout << "You have entered: ";
	for (lab=0; n<i; n++)
	  cout << lab[n] << ", ";
	delete[] p;
  }
  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Ragged Array and Pointers.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

Re: Ragged Array and Pointers.

Posted 04 December 2012 - 04:26 AM

You have a delete but no new? If you need to get the number of elements in your array you'll have to use new to allocate an array of the proper size. Also looks like you want a struct or class for your actual data.
Was This Post Helpful? 0
  • +
  • -

#3 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Ragged Array and Pointers.

Posted 07 December 2012 - 09:35 PM

Am I initializing my multidimensional arrays properly? And is there another way to work with a ragged array other than setting const int. Please help! Thank you.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

const int totlabsize = 4, lab1size = 5, lab2size = 6, lab3size = 4, lab4size = 3;

bool login(int&, int&, int&);
//the login screen

void store(int**, int, int, int);
//stores the login info

void admin(int**);
//the admin options

void display(int**);
//prints the users to the screen

int main()
{
    char done, holder;
    int *totlab[totlabsize];
    totlab[0] = new int[lab1size]; totlab[1] = new int[lab2size]; totlab[2] = new int[lab3size]; totlab[3] = new int[lab4size];

    //to start every station  as logged out
    for(int i = 0; i < lab1size; i++)
        totlab[0][i] = 0;
    for(int i = 0; i < lab2size; i++)
        totlab[1][i] = 0;
    for(int i = 0; i < lab3size; i++)
        totlab[2][i] = 0;
    for(int i = 0; i < lab4size; i++)
        totlab[3][i] = 0;

    cout << "This is to log in and out students." << endl << endl;
    do
    {
        int lab, station, idnum;

        if(login(lab, station, idnum))
            store(totlab, lab, station, idnum);
        else
            admin(totlab);
        display(totlab);
        cout << "Are you done simulating (y/n): ";
        cin >> done;
        do
            cin.get(holder);
        while(holder != '\n');
    }
    while(done != 'y' && done != 'Y');
    return 0;
}

bool login(int& a, int& b, int& c)
{
    string input;
    char comma;
    bool check;
    do
    {
        cout << "Login (lab, station, student ID) or (admin): ";
        getline(cin, input);
        if(input[0] == 'a' || input[0] == 'A')
            return false;
        stringstream(input) >> a >> comma >> b >> comma >> c;
        if(c < 10000 || c > 99999)                              //checks for a an id number of five digits
        {
            cout << "You didn't log in a person." << endl;
            check = true;
        }
        else if(a != 1 && a != 2 && a != 3 && a != totlabsize)  //checks for class
        {
            cout << "You didn't log into a class" << endl;
            check = true;
        }
        else if(a == 1 && (b < 1 || b > lab1size))              //checks for station
        {
            cout << "You didn't log into a computer" << endl;
            check = true;
        }
        else if(a == 2 && (b < 1 || b > lab2size))              //checks for station
        {
            cout << "You didn't log into a computer" << endl;
            check = true;
        }
        else if(a == 3 && (b < 1 || b > lab3size))              //checks for station
        {
            cout << "You didn't log into a computer" << endl;
            check = true;
        }
        else if(a == 4 && (b < 1 || b > lab4size))              //checks for station
        {
            cout << "You didn't log into a computer" << endl;
            check = true;
        }
        else
            check = false;
    }
    while(check);
    return true;
}

void store(int **a, int pos1, int pos2, int id)
{
    if (a[pos1-1][pos2-1] == id)
        a[pos1-1][pos2-1] = 0;
    else
        a[pos1-1][pos2-1] = id;
}

void admin(int **a)
{
    cout << "You are logged in as the administrator" << endl;
    int person;
    char done, holder;
    do
    {
        bool found(false);
        cout << "Who are you looking for: ";
        cin >> person;
        for(int i = 0; i < lab1size; i++)
        {
            if(a[0][i] == person)
            {
                cout << "Lab 1, Station " << i + 1 << endl;
                found = true;
            }
        }
        for(int i = 0; i < lab2size; i++)
        {
            if(a[1][i] == person)
            {
                cout << "Lab 2, Station " << i + 1 << endl;
                found = true;
            }
        }
        for(int i = 0; i < lab3size; i++)
        {
            if(a[2][i] == person)
            {
                cout << "Lab 3, Station " << i + 1 << endl;
                found = true;
            }
        }
        for(int i = 0; i < lab4size; i++)
        {
            if(a[3][i] == person)
            {
                cout << "Lab 4, Station " << i + 1 << endl;
                found = true;
            }
        }
        if(!found)
            cout << "None" << endl;
        cout << "Are you done as admin (y/n): ";
        cin >> done;
        do
            cin.get(holder);
        while(holder != '\n');
    }
    while(done != 'y' && done != 'Y');
}

void display(int **a)
{
    cout.setf(ios::left);
    cout << endl << "Lab Number  Computer Stations" << endl;
    cout.width(12);
    cout << "1";
    for(int i = 0; i < lab1size; i++)
    {
        cout << i + 1 << ": ";
        if(a[0][i] == 0)
            cout << "empty ";
        else
            cout << a[0][i] << " ";
    }
    cout << endl;
    cout.width(12);
    cout << "2";
    for(int i = 0; i < lab2size; i++)
    {
        cout << i + 1 << ": ";
        if(a[1][i] == 0)
            cout << "empty ";
        else
            cout << a[1][i] << " ";
    }
    cout << endl;
    cout.width(12);
    cout << "3";
    for(int i = 0; i < lab3size; i++)
    {
        cout << i + 1 << ": ";
        if(a[2][i] == 0)
            cout << "empty ";
        else
            cout << a[2][i] << " ";
    }
    cout << endl;
    cout.width(12);
    cout << "4";
    for(int i = 0; i < lab4size; i++)
    {
        cout << i + 1 << ": ";
        if(a[3][i] == 0)
            cout << "empty ";
        else
            cout << a[3][i] << " ";
    }
    cout << endl << endl;
}


This post has been edited by jimblumberg: 07 December 2012 - 09:40 PM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Ragged Array and Pointers.

Posted 07 December 2012 - 09:45 PM

You only need the const when declaring static arrays. You don't need a const when using dynamic memory.



Jim
Was This Post Helpful? 0
  • +
  • -

#5 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Ragged Array and Pointers.

Posted 07 December 2012 - 09:53 PM

How do I go about doing that? I mean what other form? I don't know how to go about that.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Ragged Array and Pointers.

Posted 07 December 2012 - 10:01 PM

Quote

How do I go about doing that?

Doing what? You may want to study up on arrays, and dynamic memory.

Statically allocated arrays array[10]; must use compile time constants (const) when declaring the sizes of these arrays.

Dynamically allocated arrays (new/delete) don't need compile time constants. You can supply this size from a variable supplied from your user, a file, a random number or many many other ways.

Jim
Was This Post Helpful? 2
  • +
  • -

#7 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Ragged Array and Pointers.

Posted 07 December 2012 - 10:08 PM

oh ok. thank you. i'm still learning arrays. when we got to arrays and now pointers and dynamic arrays it gets a little more confusing for me.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Ragged Array and Pointers.

Posted 08 December 2012 - 12:47 PM

This would be more fun with some classes, but let's assume you only know arrays and pointers.

You're torturing yourself with lab1size, lab2size, lab3size, lab4size, etc. You know arrays; this kind of thing should be setting off alarm bells.

Instead, perhaps:
const int LABS_SIZE = 4;
const int LAB_SIZE[LABS_SIZE] = { 5, 6, 4, 3 };



Now things like your init are simply a matter of:
for(int i=0; i<LABS_SIZE; i++) { totlab[i] = new int[LABS_SIZE[i]]; }



Indeed, pretty much all your code can be reduced to a quarter if you just use a lab number and pull from a second array.
Was This Post Helpful? 1
  • +
  • -

#9 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Ragged Array and Pointers.

Posted 08 December 2012 - 12:54 PM

View Postbaavgai, on 08 December 2012 - 12:47 PM, said:

This would be more fun with some classes, but let's assume you only know arrays and pointers.

You're torturing yourself with lab1size, lab2size, lab3size, lab4size, etc. You know arrays; this kind of thing should be setting off alarm bells.

Instead, perhaps:
const int LABS_SIZE = 4;
const int LAB_SIZE[LABS_SIZE] = { 5, 6, 4, 3 };



Now things like your init are simply a matter of:
for(int i=0; i<LABS_SIZE; i++) { totlab[i] = new int[LABS_SIZE[i]]; }



Indeed, pretty much all your code can be reduced to a quarter if you just use a lab number and pull from a second array.


Thank you. Thank you. Thank you. That's what I was asking. I'm trying to understand the concepts primarily I guess by asking about the syntax. That's how I come to understand code usually. Let me see if I can revise this and understand your for loop. Much appreciated.
Was This Post Helpful? 0
  • +
  • -

#10 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Ragged Array and Pointers.

Posted 08 December 2012 - 04:41 PM

Ok so i'm learning to work with char arrays. But i'm confused how to make my functions communicate properly in my main.
how do i fill my char array and use pointers to move my data about?

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

const int LABS_SIZE = 4;


void admin_menu( int, int, int );
void fill_array();
void print2screen();

//for(int i=0; i<LABS_SIZE; i++) { totlab[i] = new int[LABS_SIZE[i]]; }

void fill_array();
{

}
int main()
{
    char lab1[];
    cout << "Login (lab, station, student ID) or (admin): ";
    char lab, comp, *id = new char [5], comma;
    cout << "input";
    cin >> lab;
    if ( lab  == 'a' )
    {
      admin_menu();
      cin.ignore('\n',1000);
    }
    else
      cin >> comma >> comp >> comma >> id;
      fill_array(lab, comp, id);
      print2screen(lab, comp, id);
}
void admin_menu( int a, int b, int c )
{
    char response;
    int lab, comp, id;
    cout << "You are now logged in as the admin";
    cout << "Would you like to search for user?";
    cin >> response;
    if (response == 'Y' || response == 'y')
        for (int i=0; i<lab; i++)
            cin >> lab;
                if (lab == 1 || lab == 2 )
                    cout << "Lab 1";
                else (lab == 2 || lab == 2);
                    cout << "Lab 2";
}


Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Ragged Array and Pointers.

Posted 08 December 2012 - 04:52 PM

You may want to study the tutorials linked in my signature. They should help you understand how to properly use functions.


Jim
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Ragged Array and Pointers.

Posted 09 December 2012 - 05:55 AM

Looks like you just threw the baby out with the bath water. What happened to the code in your second post? What happened to those prototypes? What the hell does char lab1[]; mean? What does *id = new char [5] do for you that id[5] doesn't?

To have a fill array, you'd have to pass it a constructed array to fill: void fillArray(int **). Or, if you wanted another pointer in your life, you could construct the thing entirely: int **createArray().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1