2d Arrays e FUSSY - Store data in 1d area

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

2d Arrays e FUSSY - Store data in 1d area

 
0
  #1
Aug 28th, 2009
hello

I am reading a text file & trying to store some information into a 2d array.

My problem is when I go to store a piece of data into the 1st row (1d area ?) I get this error

error:
incompatible types in assignment of `float' to `float[25]'
For eg, I want to store 25.5 where x is
pur[x][];
My function:
  1.  
  2. float pur[53][25]; // Purchases Array [week number][purchase no.1, p2, pn...]
  3.  
  4. ifstream infile;
  5. float x;
  6. int count = 0;
  7. int count2 = 0;
  8.  
  9. while (infile >> x) {
  10. pur[count] = x; // error occurs here
  11.  
  12. while (x!=(char)'\n') {
  13. pur[count][count2]= x;
  14. count2++;
  15. }
  16. count2 = 0;
  17. count++;
  18. }
Last edited by gretty; Aug 28th, 2009 at 10:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,386
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 243
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #2
Aug 28th, 2009
  1. float pur[53][25];
  2. // ...
  3. pur[count] = x; // error occurs here
If pur[count] is an array of 25 floats, and x isn't, and you get an error, isn't that enough of a hint? You seem to know how to work it here:
  1. pur[count][count2]= x;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,655
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 474
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #3
Aug 28th, 2009
Improper use of an array,
  1. pur[count] = x; // Error
Try,
  1. while (infile >> x) {
  2. if(count2==25) {
  3. count2=0;
  4. count++;
  5. }
  6. pur[count][count2]= x;
  7. count2++;
  8. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #4
Aug 28th, 2009
Originally Posted by adatapost View Post
Improper use of an array,
  1. pur[count] = x; // Error
Try,
  1. while (infile >> x) {
  2. if(count2==25) {
  3. count2=0;
  4. count++;
  5. }
  6. pur[count][count2]= x;
  7. count2++;
  8. }
oh, so are you saying that I cannot store 25.5 in where x is - pur[x][]? Is that not possible when I am using a 2d array?

so I cant cant go ...
cout << pur[x];
& it will output 25.5
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: 2d Arrays e FUSSY - Store data in 1d area

 
1
  #5
Aug 28th, 2009
>Is that not possible when I am using a 2d array?
Of course not. You're using a 2D array; you need a row AND a column.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,306
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 162
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #6
Aug 28th, 2009
Also, from experience. I see that using 2D array to store information
from a file does not always work well. The program might not set
the file content it to proper location. I would suggest to use 1D array.

If 2D is really needed , then use 1D array, and copy it into 2D array.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #7
Aug 28th, 2009
Thanks, lol, I realise now I can do this so I have changed it. But now I have run into another problem.

I am tring to convert x, which is a float into an int. but I get this error
cannot convert `float' to `const char*' for argument `1' to `int atoi(const char*)'
Is there a way I can fix this without changing x to a 'const char' or 'char', ie, keep it as a float.

  1. while (infile >> x) {
  2. count = atoi(x); // error occurs here again
  3.  
  4. while (x!=(char)'\n' || count2<=25) {
  5. pur[count][count2]= x;
  6. count2++;
  7. }
  8. count2 = 0;
  9. count++;
  10. }
Last edited by gretty; Aug 28th, 2009 at 11:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #8
Aug 28th, 2009
Originally Posted by firstPerson View Post
Also, from experience. I see that using 2D array to store information
from a file does not always work well. The program might not set
the file content it to proper location. I would suggest to use 1D array.

If 2D is really needed , then use 1D array, and copy it into 2D array.
Thanks , I agree, 2d arrays are tedius, I wanted to test myself by using one(2d array) in a program but I dont really HAVE to use a 2d array. I am probably going to use a vector because it will be alot more efficient (memory-wise) & alot easier
Last edited by gretty; Aug 28th, 2009 at 11:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,306
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 162
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #9
Aug 28th, 2009
" I am tring to convert x, which is a float into an int. but I get this error"

convert float to int :

  1. float pi = 3.14159265;
  2. int pie1 = pi; //implicit conversion from float to int
  3. int pie2 = int(pi); //explicit conversion from float to int, C++ style //recommended
  4. int pie3 = (int)pi; //explicit conversion from float to int, C style

you use atoi ( a string to int ). Converts a string to int.
  1. int i = atoi("100");

Also atoi is not a good idea. Use the standard sstream header to convert between numbers to string vice-versa.
Last edited by firstPerson; Aug 28th, 2009 at 11:53 pm.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 149
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: 2d Arrays e FUSSY - Store data in 1d area

 
0
  #10
Aug 29th, 2009
thanks guys

Although I am back again with a problem, I have totally overhauled the whole function but it still uses a 2d array.

In the below code, the variable 'count' is never advanced, ie, the if statement never gets called but it should whenever we get to a line break.

  1. float x;
  2.  
  3. while (infile >> x) {
  4.  
  5. if (x==(char)'\n') { // this event never happens but it should
  6. cout << "count advanced\n";
  7. count++;
  8. pCount = 0;
  9. }
  10. else {
  11. pur[count][pCount]= x;
  12. pCount++;
  13. }
  14. }

The file I am reading is arranged like this

Week number purchase 1 purchase 2 purchase 3 purchase n

for eg.
1 2.4 1.4
32 1.4 2.7
50 8.45 2.56
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC