Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional arrays 1

Status
Not open for further replies.
May 3, 2002
633
US
Everyone here is helpful in pointing me in the correct direction with help in C coding since I am not an expert in this area, and 99% of what I do can be done in shell. However, I have something else that I am thinking needs multi-dimensional arrays since it involves a table.

What I intend is if someone type in the size of filesystem they want, say 2GB and then they type in the size of the physical partition say 16MB, it will return 128 which is the number of logical partitions needed to create the logical volume of 2GB.

Thanks for the help.

The table I intend to use is:
GB MB KB 512blk 4MB-PP 8MB-PP 16MB-PP 32MB-PP 64MB-PP
0.125 128 131072 262144 32 16 8 4
0.25 256 262144 524288 64 32 16 8
0.375 384 393216 786432 96 48 24 12
0.5 512 524288 1048576 128 64 32 16
0.625 640 655360 1310720 160 80 40 20
0.75 768 786432 1572864 192 96 48 24
0.875 896 917504 1835008 224 112 58 28
1 1024 1048576 2097152 256 128 64 32
1.125 1152 1179648 2359296 288 144 72 36
1.25 1280 1310720 2621440 320 160 80 40
1.375 1408 1441792 2883584 352 176 88 44
1.5 1536 1572864 3145728 384 192 96 48
1.625 1664 1703936 3407872 416 208 104 52
1.75 1792 1835008 3670016 448 224 112 56
1.875 1920 1966080 3932160 480 240 120 60
2 2048 2097152 4194304 512 256 128 64
2.125 2176 2228224 4456448 544 272 136 68
2.25 2304 2359296 4718592 576 288 144 72
2.375 2432 2490368 4980736 608 304 152 76
2.5 2560 2621440 5242880 640 320 160 80
2.625 2688 2752512 5505024 672 336 168 84
2.75 2816 2883584 5767168 704 352 176 88
2.875 2944 3014656 6029312 736 368 184 92
3 3072 3145728 6291456 768 384 192 96
3.125 3200 3276800 6553600 800 400 200 100
3.25 3328 3407872 6815744 832 416 208 104
3.375 3456 3538944 7077888 864 432 216 108
3.5 3584 3670016 7340032 896 448 224 112
3.625 3712 3801088 7602176 928 464 232 116
3.75 3840 3932160 7864320 960 480 240 120
3.875 3968 4063232 8126464 992 496 248 124
4 4096 4194304 8388608 1024 512 256 128

 
Another question, why in my array does the first column of the first few rows that begin with .75 or .375 for example, print 0 instead of the .75?

Row = 0, Col = 0, Value = 0
Row = 0, Col = 1, Value = 128
Row = 0, Col = 2, Value = 131072
Row = 0, Col = 3, Value = 262144
Row = 0, Col = 4, Value = 32
Row = 0, Col = 5, Value = 16
Row = 0, Col = 6, Value = 8
Row = 0, Col = 7, Value = 4
Row = 1, Col = 0, Value = 0

The code:
#include <stdio.h>

int main() {
int x, y;

int grid[32][8] = { {0.125,128,131072,262144,32,16,8,4},{0.25,256,262144
,524288,64,32,16,8},{0.375,384,393216,786432,96,48,24,12},{0.5,512,524288,104857
6,128,64,32,16},{0.625,640,655360,1310720,160,80,40,20},{0.75,768,786432,1572864
,192,96,48,24},{0.875,896,917504,1835008,224,112,58,28},{1,1024,1048576,2097152,
256,128,64,32},{1.125,1152,1179648,2359296,288,144,72,36},{1.25,1280,1310720,262
1440,320,160,80,40},{1.375,1408,1441792,2883584,352,176,88,44},{1.5,1536,1572864
,3145728,384,192,96,48},{1.625,1664,1703936,3407872,416,208,104,52},{1.75,1792,1
835008,3670016,448,224,112,56},{1.875,1920,1966080,3932160,480,240,120,60},{2,20
48,2097152,4194304,512,256,128,64},{2.125,2176,2228224,4456448,544,272,136,68},{
2.25,2304,2359296,4718592,576,288,144,72},{2.375,2432,2490368,4980736,608,304,15
2,76},{2.5,2560,2621440,5242880,640,320,160,80},{2.625,2688,2752512,5505024,672,
336,168,84},{2.75,2816,2883584,5767168,704,352,176,88},{2.875,2944,3014656,60293
12,736,368,184,92},{3,3072,3145728,6291456,768,384,192,96},{3.125,3200,3276800,6
553600,800,400,200,100},{3.25,3328,3407872,6815744,832,416,208,104},{3.375,3456,
3538944,7077888,864,432,216,108},{3.5,3584,3670016,7340032,896,448,224,112},{3.6
25,3712,3801088,7602176,928,464,232,116},{3.75,3840,3932160,7864320,960,480,240,
120},{3.875,3968,4063232,8126464,992,496,248,124},{4,4096,4194304,8388608,1024,5
12,256,128} };

for(x=0; x < 32; x++) {
for(y=0; y < 8; y++) {
printf(&quot;Row = %i, Col = %i, Value = %i\n&quot;, x, y, grid[x]
[y]);
}
}
}


 
I'll offer the easy one. You're declaring the whole array as integers, and C truncates floating point numbers, effectively rounding everything down. Try declaring the array as float and printing &quot;%f&quot;. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
This is a skeleton that could be used to construct a
linked list idea that would be pretty simple to use.
Code:
#include <stdio.h>
#include <stdlib.h>


struct SIZEDRIVE {
float size;
void (*makeCalc)(float);
};

void answers(float);
 

int main(void) {
struct SIZEDRIVE drv;

        while (1) {
             printf(&quot;Enter size of drive: &quot;);
             scanf(&quot; %f&quot;, &drv.size);
             drv.makeCalc = answers; drv.makeCalc(drv.size);
        }

return 0;
}
             
              
                       
void answers(float base) {
     printf(&quot;Size of drive: %f\n&quot;, base);
     printf(&quot;MB: %f\n&quot;, base * 1024);
     printf(&quot;KB: %f\n&quot;, base * 1024 * 1024);
     printf(&quot;512 K blocks: %f\n&quot;,base * 1024 * 1024 * 2);
     printf(&quot;4MB pp: %f\n&quot;, base * 1024 / 4);
     printf(&quot;8MB pp: %f\n&quot;, base * 1024 / 8);
     printf(&quot;16MB pp: %f\n&quot;, base * 1024 / 16);
}
Sample run:
Enter size of drive: 3.38
Size of drive: 3.380000
MB: 3461.120117
KB: 3544187.000000
512 K blocks: 7088374.000000
4MB pp: 865.280029
8MB pp: 432.640015
16MB pp: 216.320007

Good Luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top