GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Apr-2004, 18:30
oshiotse oshiotse is offline
New Member
 
Join Date: Apr 2004
Posts: 14
oshiotse is on a distinguished road

array size


pls help!!

Take for example, u have a count of 10 number. how can this count be declared as the array size
is it
CPP / C++ / C Code:
#define size count
int arr[size];

i am trying to make the size more dynamic
  #2  
Old 28-Apr-2004, 18:46
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by oshiotse
pls help!!

Take for example, u have a count of 10 number. how can this count be declared as the array size
is it
CPP / C++ / C Code:
#define size count
int arr[size];

i am trying to make the size more dynamic

Either you can do #define count 10 or #define size 10
and then int arr[count] or int array[size]
or in c++ you can do const int size = 10 then,
int array[size]
but you can't do any of this dynamically, for example
cout<<"enter size";
cin>>size;
int arr[size]; //error

for dynamic storage, u'd have to use linked lists or some other pointer based data structure.
  #3  
Old 28-Apr-2004, 20:16
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Machinated is correct. You can't define an array to be dynamic. However, you can define a dynamic structure and still address it like an array.

For example:
CPP / C++ / C Code:
#include "stdlib.h"
#include "stdio.h"

int main()
{
  int* array;
  int number;
  int index; 

  printf("How many integers do you want to enter? ");
  scanf("%d",&number);
  array = (int*) malloc(number*sizeof(int));
  for(index=0;index<number;index++){
    printf("Enter #%d: ",index);
    scanf("%d",&array[index]);
  }
  printf("\nHere are your numbers\n");
  for(index=0;index<number;index++)
    printf("Number #%d is %d\n",index,array[index]);
 }

HTH,
d
  #4  
Old 01-May-2004, 04:46
tay's Avatar
tay tay is offline
Junior Member
 
Join Date: Jan 2004
Posts: 77
tay will become famous soon enough
this is dynamic array,
it can automatic change the array size when your input data is more than your array size
i present it using class in C++ form
CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
using namespace std;

class MyArray {
 public:
   MyArray() : capacity(4), size(0) { // initial array size is 4 or int data[4]
     data = new int[capacity];
   }
   void add(int n) { //input data function
     if (size==capacity)  // if input data is = max size of array
        increaseCapacity();// resize the array
     data[size] = n;
     size++; // counter the array size
   }
   int get(int index) {
     return data[index];
   }    
   void set(int index, int value) {
     data[index] = value; 
   }    
   void display() {
     for (int i=0;i<size;i++)
        cout << data[i] << " ";
   }
 private:
   void increaseCapacity() {  // increase array size when input > 4
     capacity *= 2; // increase array size to double;array size is data[8]

     int* newdata = new int[capacity]; // create new array
     for (int i=0;i<size;i++)
        newdata[i] = data[i]; // copy all data from old array to new array
     delete[] data; // delete data memory allocated in int *data
     data = newdata;// copy all data from newdata to data again
  }// once exit this fucntion, newdata will automatic deleted
  int* data; // data pointer to memory
  int size, capacity;
};

int main() {
 MyArray m;
 for (int i=0;i<10;i++) // input data more than 4
    m.add(i*2);
 m.display();   
}
Last edited by dsmith : 01-May-2004 at 07:08. Reason: Please use [c] & [/c] for syntax highlighting
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
Re: Programming Techniques WaltP C Programming Language 0 09-Mar-2004 23:56
c: array comparison jack C Programming Language 7 26-Jan-2004 11:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.