Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can i get help on C++ arrays ?
New Posts  All Forums:Forum Nav:

Can i get help on C++ arrays ?

post #1 of 13
Thread Starter 
Hey im suppose to do this simple task. My prof wants me to find out how many zeros are in the array and she gave me int num [100]...

So she wants me to write a for loop using int num [100]

In other words write a for loop to count how many zeros are in the array. Can anybody help explain this a little better ? or how do you even do this?
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #2 of 13
What part are you confused about?
post #3 of 13
use a for loop like this:
for(i=0; i<arraySize; i++)
access array element at location i
compare that element to 0
if its 0, increment some counter for number of zeros
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
post #4 of 13
Thread Starter 
Quote:
Originally Posted by mott555 View Post
What part are you confused about?
Well i'm fairly new to programming i'm trying to find out how would you do this?
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #5 of 13
Basically, you are going to:

write a loop which will iterate 100 times //or better yet, replace 100 with the size of the array
then check to see if each element at the array(index of the loop counter) is equal to 0
if it is, add one to a counter that keeps track of the zeros

once the loop runs you should be good.

I wrote the program if you still struggle, but first try to write it. If you have issues, post you code here.
post #6 of 13
Quote:
Originally Posted by Aknolidge View Post
Well i'm fairly new to programming i'm trying to find out how would you do this?
Do you know how to use a for loop? If so travesty's post sums it up pretty well.
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #7 of 13
Thread Starter 
Quote:
Originally Posted by travesty View Post
use a for loop like this:
for(i=0; i<arraySize; i++)
access array element at location i
compare that element to 0
if its 0, increment some counter for number of zeros
yeah i figured that but i didnt know if i was right or wrong i had

for loop ( i= 0; i <= 100 ; i++)

after that i was stuck
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #8 of 13
Thread Starter 
thanks for the help everyone
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
overclocking toy
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7-950 Asus P6TD Deluxe MSI GTX 560 TI Corsair XMS3 12gb DDR3 
Hard DriveOptical DriveOSMonitor
1.5 TB @7200 RPM Blu-Ray/DVD/CD + Burner Windows 7 Asus 27" LED 
PowerCase
Corsair HX 850 Coolermaster HAF 932 
  hide details  
Reply
post #9 of 13
Just for future reference, if you know the size of your array using for(i=0;i<100;i++) is ok, but it would be better practice to use for(i=0;i<array.size();i++) in case the size of your array changes. Anyway, good luck with programming!
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #10 of 13
Quote:
Originally Posted by superhead91 View Post
Just for future reference, if you know the size of your array using for(i=0;i<100;i++) is ok, but it would be better practice to use for(i=0;i<array.size();i++) in case the size of your array changes. Anyway, good luck with programming!
Its been awhile since I have done C++, but don't you have to do:

Code:
int arrSize = sizeof(arr) / sizeof(int);
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Can i get help on C++ arrays ?