Jump to content

User declared Arrays

- - - - -

  • Please log in to reply
11 replies to this topic

#1
jairopena

jairopena

    Newbie

  • Members
  • Pip
  • 4 posts
Hi all.

I'm an MIS major whos trying to maintain his programming skills (as my job in not related to programming).

Basically, i need help creating a dynamic array.

The idea is, to allow the user to input data (ages, scores, etc), as many times as needed. Here is a semi generic code snippet that i've come up with.


#include<stdio.h>


int main()


{ 

     int i;                                             /* declare counter */

     int age[];                                       /* declare array "age" */

       do {                                            /* initialize array */

             i=i+1;                                   /* tracks iterations*/

             printf("Enter age");                 /* prompt user for value*/

             scanf("%d", age[i]);}              /*accept input from user*/

       while(age[i] != 0);                       /* terminate loop when user inputs 0*/

       return 0;                                     /* return 0 if no errors*/


}


Everything ive come across on the web assumes that the user knows how big the array needs to be. I'm sure C can handle this, but i don't know if there is a specific function i should use, or if i have to work around it.

Any help is appreciated.

Thanks

Edited by dargueta, 20 January 2011 - 12:03 AM.
Added code tags


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you don't know how big the array will need to be, you will need to use a pointer and allocate the necessary memory on the fly. Alternatively, you can use a data structure like a linked list.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You can always make an array with 1000000 elements (if we assume that int is 4 Bytes that consumes 4 MB of RAM). Or you could dynamiclly allocate memory as user types in (probably should deteremine a stop character). For that you'd need 2 arrays (might be possible with 1 but I don't know how); store input in A1, increase A2 by 1 and copy contents of A1 to A2 and then do vice versa. But that's consuming a lot of CPU so I wouldn't recommend it.

So that leaves us with 3rd option, linked list. I guess you could imagine linked list as a train with lots of train-cars (thingys behind the locomotive). So, if you wanna go with trains say so and we'll start the ride. :D (I assumed you haven't heard of linked list before)

EDIT: Didn't see Winged Panther's reply... **** this wall of text!
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
jairopena

jairopena

    Newbie

  • Members
  • Pip
  • 4 posts
I've head of linked lists, but only vaguely. I think i will go with option 2, since pointers are something i will inevitbly use in the future, but only know the very basics.

Can you provide sample code?

#5
jairopena

jairopena

    Newbie

  • Members
  • Pip
  • 4 posts
Sorry, after reading your reply a second time, i'll take your word for it and go with option 3. Thanks

#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Linked lists use pointers anyway. You could use a struct for a linked list:

typedef struct

{

    int value;

    node *nextnode;

} node;

Basically, what you need to do to store something in a linked list is to allocate another node, store the data in that node, and set *nextnode in the previous node to a pointer to the current node. Then, when you want to get a certain element, you start from the first node, and get the pointer from it. You then get the pointer from the node you just got the pointer for. And you repeat until you have reached the desired element.
Latinamne loqueris?

#7
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
I recommend you first review basic C as it applies to arrays, then review the data-structures C++ provides you. Vector is what you are looking for here, not a linked a list. A sample of your program should look more like this. Note indentation, use of predefined data-structures, and commenting using // not /* */.


#include <iostream>

#include <vector>


int main()

{

  std::vector<int> Ages(0);

  int UserInputAge = -1;


  while(UserInputAge != 0)

  {

    std::cin >> UserInputAge;

    // Preform additional validation here (eg. UserInputAge > 0) if you wish.

    Ages.push_back(UserInputAge);

  }


  Ages.remove(Ages.end()); // Assuming you do not preform additional validation to prevent 0 from being added to the vector


  return (0);

}



#8
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
Linked lists use pointers, so you will have to know pointers to use a linked list.

consider using vectors. they are dynamic arrays in c++

they are located within the namespace "std" and they are located within a file named vector.h

#include <vector.h>

int main(){

std::vector<int> age;

age.push_back( 10 );

age.push_back( 44 );

age.push_back( 27 );


for( int i = 0; i < age.size(); i++ )

{

printf( "Person %d's age is %d\n", i + 1 , age.at(i) );

}


}

which should output:
Person 1's age is 10

Person 2's age is 44

Person 3's age is 27

edit: we were writing the post at the same time, but yours came up first sam :) good job.

#9
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts

Skippy said:

they are located within the namespace "std" and they are located within a file named vector.h

#include <vector.h>
NOT in "vector.h" but in "vector". All standard library header files have no ".h" extension.

OP wrote about C and not C++, can he clarify if it indeed must be C? If not, then STL containers are the way, but if so, then there's no point discussing them here.

#10
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts

Quote

NOT in "vector.h" but in "vector". All standard library header files have no ".h" extension.

Thank you for the correction.
And this is true, I was just assuming C++ could be used. But if C was a requirement, then as Flying Dutchman said, linked lists are also an option.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#11
jairopena

jairopena

    Newbie

  • Members
  • Pip
  • 4 posts
Im focused primarily on C, but what im getting from this is that it may be time to upgrade to C++

#12
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
C++ is a lot nicer to programmers than C when it comes to this sort of stuff :)
It has libraries of classes which.. are rather small compared to the libraries of functionality in python, Java, etc... but still C++ implements just a small bit of that for you, which makes your job programming a lot easier.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users