Jump to content

Storing numbers in a character array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
20 replies to this topic

#1
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
I'm writing a program that needs to take in a 2000 x 2000 multidimensional array.

All the entries in the array are numbers between -1 and 100.

So I use a nested loop to take all the numbers and store them in the array. But when I output it gives me all single characters, or it will give me the ASCII character. Not the number.

#2
CPD

CPD

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts

Quote

I'm writing a program that needs to take in a 2000 x 2000 multidimensional array.
That's a lot of space. Do you have to have all of the numbers in memory at once?

Quote

But when I output it gives me all single characters, or it will give me the ASCII character. Not the number.
I guess you're using an array of char to hold the numbers? char is treated specially on the assumption that you want a character and not a small integer, so you need to do some casting to int when printing.

#3
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
I do have all the numbers in memory at once. I realize it's taking up about 4MB of memory.

And I will try the cast thing. Thanks.

#4
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
Yep, that helped! I also found out another problem. The program was storing the -1 as 2 characters.

The first one was '-' and the second was '1'. So I fixed that by inputting into an int, then storing the int as a char.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
A nonstandard way of fixing that would be to declare the array as an array of __int8. I'm pretty sure that's just a Microsoft extension.

char myData[2000][2000]; //requires typecasting
__int8 myOtherData[2000][2000]; //no typecasting

__int8 is signed by default.

#6
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
My computer prof requires us to compile and run our programs under Fedora Linux. Thanks though, I'll keep that in mind.

#7
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
From my base/types.h file, these are guaranteed to be the proper size by C/C++

typedef unsigned int   u32int;
typedef          int   s32int;
typedef unsigned short u16int;
typedef          short s16int;
typedef unsigned char  u8int;
typedef          char  s8int;


#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,713 posts
C99 specifies minimum widths. These could be off.

#9
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Check out 7.18.1.1 and 7.18.1.2 for information on the exact specifications. Although it is basically implementation defined, but required if the width exists.

Format: intN_t and uintN_t ; where N is the width

#10
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
Sorry guys, these last 3 messages are over my head. :P

Beyond what I've learnt in the class so far.

#11
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Here's a question for you ^^ Is this in C or C++.

#12
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
For clarification, my post is based on C99.