New Posts  All Forums:Forum Nav:

Java to C

post #1 of 5
Thread Starter 
Hey there im trying to put a code that i can do in java into c.

Code:

for(int r=0; r<array.length; r++){
for(int c=0; c<array[r].length; c++){
System.out.print(array[r][c] + " ");
}
System.out.println();
}
that is java and it was for an old assignment now i am trying to use 2d arrays in c but initializing the array with out using array[19] = {....,....,...}
is turning out to be a problem so far i have
Code:
int r,c;
for(r=0; r<sizeof(board)/sizeof(int); r++){
         for(c=0; c<board[r]
but i dont know how to put array length properly after board[r].
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #2 of 5
the Java code snippet is printing the contents of the array, while i don't see what you are trying to do with the C snippet? what are you trying to initialize the array to?
Seanbot X
(10 items)
 
  
CPUMotherboardGraphicsRAM
i7-3930K Asus Rampage IV Gene Asus GTX 980 Ti STRIX Mushkin Redline  
Hard DriveCoolingOSMonitor
WD Red Corsair H100i Windows 10 Monoprice ZeroG Slim 1440p 
PowerCase
Corsair AX1200 Corsair 350D 
  hide details  
Reply
Seanbot X
(10 items)
 
  
CPUMotherboardGraphicsRAM
i7-3930K Asus Rampage IV Gene Asus GTX 980 Ti STRIX Mushkin Redline  
Hard DriveCoolingOSMonitor
WD Red Corsair H100i Windows 10 Monoprice ZeroG Slim 1440p 
PowerCase
Corsair AX1200 Corsair 350D 
  hide details  
Reply
post #3 of 5
I think there might be a more fundamental misunderstanding here.

The "array" in the java code is actually an object, where in C, there are no objects. Calling the function "length" on the array object returns the length of the array. In C, there is no equivalent, you must know the length of the array before operating on it.

A C equivalent code snippet of the code you posted in Java would be the following:

Quote:
int array2[10][10];

for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
printf("Array element <%i,%i> is %i\
", row, col, array2[row][col]);
}
}
If you have any questions about the code, feel free to ask!
Graddy
(13 items)
 
  
CPUGraphicsRAMHard Drive
2.93 Ghz Core 2 Duo 9400m + 9600m GT 4GB Apple RAM 320 GB 
OSMonitorCase
Mac OS X + Windows 7 Professional 24" Apple Display (1920x1200) Unibody Alumimum 
  hide details  
Reply
Graddy
(13 items)
 
  
CPUGraphicsRAMHard Drive
2.93 Ghz Core 2 Duo 9400m + 9600m GT 4GB Apple RAM 320 GB 
OSMonitorCase
Mac OS X + Windows 7 Professional 24" Apple Display (1920x1200) Unibody Alumimum 
  hide details  
Reply
post #4 of 5
Thread Starter 
that code does the same thing? In class we learned that the c<board[r].length was the way to check each column properly because you go to row 1 then c1,c2,c3 etc then row 2 c1, c2 ,c3 etc. How does that c code do the same thing?
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #5 of 5
there is no .length operator in c.

i think this is what you're looking for:
Code:
int length = sizeof(board[r])/sizeof(int);
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming