473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Array of Strings

Hi

I am stuck at this....basically i am very new to c+ programming and would like to know how to create a multi dimensional array of strings.

Also i would like to create a default multidimensional array of strings of the size mxn = 3000 x 5000. Is it possible in C++. can u give me suggestions on this as well....

Many thanks

Vinay
Sep 17 '07 #1
16 2211
r035198x
13,262 8TB
Hi

I am stuck at this....basically i am very new to c+ programming and would like to know how to create a multi dimensional array of strings.

Also i would like to create a default multidimensional array of strings of the size mxn = 3000 x 5000. Is it possible in C++. can u give me suggestions on this as well....

Many thanks

Vinay
Have you read any tutorial on strings and/or arrays in C++ yet?
Sep 17 '07 #2
Savage
1,764 Expert 1GB
Hi

I am stuck at this....basically i am very new to c+ programming and would like to know how to create a multi dimensional array of strings.

Also i would like to create a default multidimensional array of strings of the size mxn = 3000 x 5000. Is it possible in C++. can u give me suggestions on this as well....

Many thanks

Vinay
Using c arrays that would be:

string array[m][n]....[x];

using c++ STL's vector it would be much more complicated,it's better for now that you use it like this(until you find some good books or tutorials as our admin suggested).

About your second question,typedef it:

typedef name string[3000][5000];

Savage
Sep 17 '07 #3
Using c arrays that would be:

string array[m][n]....[x];

using c++ STL's vector it would be much more complicated,it's better for now that you use it like this(until you find some good books or tutorials as our admin suggested).

About your second question,typedef it:

typedef name string[3000][5000];

Savage

Thanks a lot....


Is it possbile to have array upto sizes of 3000 x 5000 in Visual C++?? Cause when i try the same thing with char; it stops at 100 x 5000.

anyways i will try it...

thnx
Sep 17 '07 #4
Savage
1,764 Expert 1GB
Thanks a lot....


Is it possbile to have array upto sizes of 3000 x 5000 in Visual C++?? Cause when i try the same thing with char; it stops at 100 x 5000.

anyways i will try it...

thnx
It all depends on your RAM

If you have more of it,you can have bigger arrays(Or if you have lots of free RAM).

Savage
Sep 17 '07 #5
It all depends on your RAM

If you have more of it,you can have bigger arrays(Or if you have lots of free RAM).

Savage
Thanks savage

I have 512 Mb Ram right now....outta which i shud be able to spare 200 mb easily at one time....Also I am using dev c++ right now.. do you think that creates a problem?

I have developed a following mini program which shud specify the size of an array....it works for normal int and char types....but doesnt work for a sting type....can u suggest any changes?

#include <iostream>
#include <stdio>
#include <string>
using namespace std; // I dont really know what this means.....

int main()

{
string a[30][50];
std::cout<<sizeof(a);

std::cin.get();

}


thnks
Sep 17 '07 #6
Savage
1,764 Expert 1GB
Thanks savage

I have 512 Mb Ram right now....outta which i shud be able to spare 200 mb easily at one time....Also I am using dev c++ right now.. do you think that creates a problem?

I have developed a following mini program which shud specify the size of an array....it works for normal int and char types....but doesnt work for a sting type....can u suggest any changes?

#include <iostream>
#include <stdio>
#include <string>
using namespace std; // I dont really know what this means.....

int main()

{
string a[30][50];
std::cout<<sizeof(a);

std::cin.get();

}


thnks
Ok,lets see:

1.Please use code tags(select your code in Message Editor and click on # button)

2.using namespace std: what this means is that you are using functions from that namespace,when you have it declared you don't need to use std::cout,std::cin,you can just use cout,cin...

For future info you can check C++ articles zone.In that blue menu bellow "thescripts developer network",choose articles and then navigate to c++.
Then choose "Programming classes by Gannon11".

Right now I don't have a compiler at my hand,but can you tell me how it doesn't work.Is there any error,do you get something strange from sizeof?

Savage
Sep 17 '07 #7
Ok,lets see:

1.Please use code tags(select your code in Message Editor and click on # button)

2.using namespace std: what this means is that you are using functions from that namespace,when you have it declared you don't need to use std::cout,std::cin,you can just use cout,cin...

For future info you can check C++ articles zone.In that blue menu bellow "thescripts developer network",choose articles and then navigate to c++.
Then choose "Programming classes by Gannon11".

Right now I don't have a compiler at my hand,but can you tell me how it doesn't work.Is there any error,do you get something strange from sizeof?

Savage

ok....i did as u suggested...

I used the following code...

Expand|Select|Wrap|Line Numbers
  1.     #include <iostream>
  2.  
  3.     #include <string>
  4.     using namespace std;
  5.  
  6.     int main()
  7.  
  8.     {
  9.         string a[30][50];
  10.         cout<<sizeof(a);
  11.  
  12.         cin.get();
  13.  
  14.     }

Now i am getting the output window showing a result of 600000. When i use dimensions more than 300 x 500, it just gives me windows not responding error......does it have to do with the ram of my system...or is it somethin else?

Also how many characters by default can u use in every attribute in the array...?

thnx again.....u have helped me a lot...
Sep 17 '07 #8
Savage
1,764 Expert 1GB
ok....i did as u suggested...

I used the following code...

Expand|Select|Wrap|Line Numbers
  1.     #include <iostream>
  2.  
  3.     #include <string>
  4.     using namespace std;
  5.  
  6.     int main()
  7.  
  8.     {
  9.         string a[30][50];
  10.         cout<<sizeof(a);
  11.  
  12.         cin.get();
  13.  
  14.     }

Now i am getting the output window showing a result of 600000. When i use dimensions more than 300 x 500, it just gives me windows not responding error......does it have to do with the ram of my system...or is it somethin else?

Also how many characters by default can u use in every attribute in the array...?

thnx again.....u have helped me a lot...
Well,300x500 is about 57.22Mb of RAM.

I suppose that stack arrays have limited amount of memory(If I'm not wrong it was 4096 bytes in C)

Try allocating it dynamicly:

Expand|Select|Wrap|Line Numbers
  1. string **a; //pointer to pointer to a string
  2. int x=300;
  3. int y=500;
  4.  
  5. a=new string*[x] //Allocates first dimension
  6. for(int i=0;i<x;i++)
  7. {
  8.   a[i]=new string[y];//Allocates sub array
  9. }
  10.  
and then you can use as a normal array.

After you have completed everything you need with string a[x][y],because it was dynamicly allocated you must deallocate it:(Reverse of allocation process)

Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<x;i++)
  2. {
  3.   delete[] a[i];//deletes sub array 
  4. }
  5.  
  6. delete[] a;//Delete first dimension
  7.  
You must allocate it and dealocate it like this because heap memory is not contiguous as stack memory.

Tip:by adding to [code] tag ="language",you can achieve syntax highlighting.

e.g:[code="cpp"]

Savage
Sep 17 '07 #9
Well,300x500 is about 57.22Mb of RAM.

I suppose that stack arrays have limited amount of memory(If I'm not wrong it was 4096 bytes in C)

Try allocating it dynamicly:

Expand|Select|Wrap|Line Numbers
  1. string **a; //pointer to pointer to a string
  2. int x=300;
  3. int y=500;
  4.  
  5. a=new string*[x] //Allocates first dimension
  6. for(int i=0;i<x;i++)
  7. {
  8.   a[i]=new string[y];//Allocates sub array
  9. }
  10.  
and then you can use as a normal array.

After you have completed everything you need with string a[x][y],because it was dynamicly allocated you must deallocate it:(Reverse of allocation process)

Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<x;i++)
  2. {
  3.   delete[] a[i];//deletes sub array 
  4. }
  5.  
  6. delete[] a;//Delete first dimension
  7.  
You must allocate it and dealocate it like this because heap memory is not contiguous as stack memory.

Tip:by adding to [code] tag ="language",you can achieve syntax highlighting.

e.g:[code="cpp"]

Savage

hey

thanks....this works for now....

but will this work when i need the whole 300 X 500 attributes at once.....cause i need to input that many values for my program....(someting like getting input as all the search results from google!!!!)
Sep 17 '07 #10
Savage
1,764 Expert 1GB
hey

thanks....this works for now....

but will this work when i need the whole 300 X 500 attributes at once.....cause i need to input that many values for my program....(someting like getting input as all the search results from google!!!!)
I'm not sure that I understood this.

Can you rephrase it or show me some simple code example?

Savage
Sep 17 '07 #11
I'm not sure that I understood this.

Can you rephrase it or show me some simple code example?

Savage

ok...i do not have a concrete example right now as i have just started programming for the program....but i will try explaining it again!..

basically if i want to input a whole set of values intead of inputting values one by one, will the dynamic allocation for the string array work? or do I need to get more memory and declare the whole string array at once....??
Sep 17 '07 #12
Savage
1,764 Expert 1GB
ok...i do not have a concrete example right now as i have just started programming for the program....but i will try explaining it again!..

basically if i want to input a whole set of values intead of inputting values one by one, will the dynamic allocation for the string array work? or do I need to get more memory and declare the whole string array at once....??
Of course it will.Also you can change size of dynamic array at the run time.Say that you are reading in file that contains 10 rows and 3 columns of strings.Why allocate 300x500 when you can just allocate 10x3,this will ensure that you have more free memory and that you don't use more memory than you need to.

Savage
Sep 17 '07 #13
Of course it will.Also you can change size of dynamic array at the run time.Say that you are reading in file that contains 10 rows and 3 columns of strings.Why allocate 300x500 when you can just allocate 10x3,this will ensure that you have more free memory and that you don't use more memory than you need to.

Savage

ohk....thats sooo coooL!!!..

thanks man....i have just started programming and this helps a lot....

I will keep in touch...

cheers
Sep 17 '07 #14
Savage
1,764 Expert 1GB
ohk....thats sooo coooL!!!..

thanks man....i have just started programming and this helps a lot....

I will keep in touch...

cheers
I'm more than happy to help you.

And,of course,feel free to post again if you find yourself stuck.

Savage
Sep 17 '07 #15
hello

is it possible to use the same syntax to declare a float array? just by changing the data type from string to float?
Dec 7 '07 #16
Also if i want to input some values in the array using for functions or maybe call the array from another function in the form a[x][y]; is it possible? really apperciate ur help!

thnx
Dec 7 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
3
by: Morten71 | last post by:
I have a strange problem. I have a local string() var I populate this way: clmns() As String = {"InvoiceNo", "InvoiceDate"} When I call: Array.IndexOf(clmns,"InvoiceDate") I get 0 (zero) as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.