3 Replies - 407 Views - Last Post: 29 December 2012 - 02:57 PM Rate Topic: -----

#1 donfire  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 17
  • Joined: 26-December 12

ARRAY task, pointer dynamic memory?

Posted 29 December 2012 - 05:34 AM

am using c++.
i have this code which prints out a result like this on the screen ;

first numbers are ; 23, 25, 56, 75, 65,

second numbers are; 45, 87, 90, 1, 76,

third numbers are; 23, 34, 65, 87, 2,

is there a way i can make the program read all this numbers into a one single array at runtime? i wish to have an array of lets say

int possible []= {23, 25, 56, 75, 65, 45, 87, 90, 1, 76, 23, 34, 65, 87, 2};

using cin and some magic call or code hahaha :) may be pointer and dynamic memory? i cant figure it out, or am i asking too much? is manually creating the array the only solution?

Is This A Good Question/Topic? 0
  • +

Replies To: ARRAY task, pointer dynamic memory?

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: ARRAY task, pointer dynamic memory?

Posted 29 December 2012 - 06:22 AM

So, how exactly do you want to read the numbers? You want to read them from stdin? If you don't know how many values will be read at compile time, you could use an std::vector which can be easily resized dynamically.

This post has been edited by Aphex19: 29 December 2012 - 06:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,407
  • Joined: 25-December 09

Re: ARRAY task, pointer dynamic memory?

Posted 29 December 2012 - 07:52 AM

Please show some code showing what you have tried to do to solve your problem. You may want to study the following link: Arrays for a simple tutorial on using arrays.

Also there is no "some magic call or code" to do the work for you, you must write the code to populate your array or other data structure.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: ARRAY task, pointer dynamic memory?

Posted 29 December 2012 - 02:57 PM

Quote

am i asking too much


Your asking too much, there is not magical function call to do this. You can create dynamic memory for holding all of the integer values; however, if you know the maximum possible integers being inputted at compile time, then you can use a regular array allocated on the stack.

#include <iostream>

#define MAX 20

using namespace std;

int main(int argc, char *argv[]) {
   int input[MAX];
   for(int i=0; i<MAX && std::cin>>input[i]; i++);
      
   return 0;
}


This post has been edited by jjl: 29 December 2012 - 02:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1