• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

More noob C++ programmer help

Joined
Aug 2, 2009
Messages
156 (0.05/day)
Likes
14
Location
Washington The State
System Name Tiny Computer
Processor i7 2600k
Motherboard ASRock H67M-ITX
Cooling Stock for now
Memory 8 GB G.skill
Video Card(s) Unlocked 6950 @ 910/1400
Storage WD Black 1 TB (FAEX)
Display(s) 24" Dell 1920x1080
Case SG07
Power Supply Silverstone 600W
Software Windows 7 x64 Pro
#1
Hey guys, one last problem here.

I have this function for outputting an array

// ===================
// Function PrintArray
// =============================
void PrintArray( string name,
int data[],
int numberOfElements ){

int ii; // Loop Control Variable

cout << endl;

ii = 0;
while ( ii < numberOfElements ){
cout << name << "[" << ii << "] = "
<< data[ii] << endl;
ii++;
} // While loop

cout << endl;

} // Function PrintArray
// ========================
As it is, it outputs one element per line. Is there a way to make it output 10 elements per line?
 
Joined
Dec 2, 2009
Messages
3,257 (1.10/day)
Likes
315
System Name Mercury KM-81
Processor Phenom II x4 B50
Motherboard ASUS M4A89GTD PRO
Cooling Cooler Master Hyper TX3
Memory 6GB Ram (4GBx1 Corsair 2GBx1 Nanya)
Video Card(s) PowerColor AX6770 V2.0
Storage 2TB WD Black, 1TB Hitachi, 500 GB WD Blue
Display(s) Philips 247E-LPH 24" 1920x1080
Case Mercury
Audio Device(s) Integrated Realtek
Power Supply Corsair 750TX
Software Windows 7 64-bit
Benchmark Scores Maybe it is time to benchmark :D
#2
The only way to output 10 lines is to get a library that integrates in the C++ as a function and does the same think again and again. Only experts do that. I did it with Nvidia's link libraries and it should always start like <stdio.h> or in this case <repeat.h>. The sqrt for example could not be known as a command if u dont put the <math.h> at the start below #include#. Maybe u can do that yourself, integrating the dll in your C++, but it needs specific commands which u need to know before using it. Otherwise.... ERROR
 
Joined
Feb 11, 2008
Messages
537 (0.15/day)
Likes
164
Location
Lincoln, Nebraska, USA
System Name This PC
Processor Intel Core i7 2600K @ 4.8 GHz
Motherboard ASUS P8Z68 Deluxe
Cooling Custom Water Cooling - CPU Only
Memory 8GB (2 x 4GB) AData DDR3
Video Card(s) GTX 1080 TI - EVGA FTW3 - Slave BIOS
Storage Corsair Force GT 128GB / Samsung 850 Pro 512 GB
Display(s) AOC AGON AG271QG - 165 Hz - AHVA
Case Dimastech v2.5 Easy
Audio Device(s) Onboard
Power Supply Antec 1.2kW HCP
Mouse Logitech G403 - Wired
Keyboard Rosewill - MX Browns
Software Windows 10 Pro x64
#3
If I understand correctly what you are asking, yes you can have ten outputs per line. Think about those simple c++ programs that make pyramids and stuff with numbers. I can give you further information if you need it, but a hint for right now is that it would require nested for loops.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.46/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#4
Create a variable to do the ten count. Reset the variable when it reaches 10. No need for nested loops.
Code:
ii = 0;
tenloop = 1;
while (ii < numberOfElements)
{
    cout << stuff to output with no endl;
    ii++;
    tenloop++;
    if (tenloop == 11)
    {
          cout << endl;
          tenloop = 1;
    }
}
 
Joined
Aug 2, 2009
Messages
156 (0.05/day)
Likes
14
Location
Washington The State
System Name Tiny Computer
Processor i7 2600k
Motherboard ASRock H67M-ITX
Cooling Stock for now
Memory 8 GB G.skill
Video Card(s) Unlocked 6950 @ 910/1400
Storage WD Black 1 TB (FAEX)
Display(s) 24" Dell 1920x1080
Case SG07
Power Supply Silverstone 600W
Software Windows 7 x64 Pro
#5
Awesome, that's exactly what I needed.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.46/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#6
When learning to program, it's sometimes difficult to wrap your head around the logic that you need to perform a given function. Stop and think it though.
The idea is to make it as simple as possible and write the least code you have to, to accomplish the task at hand. There are many ways to code things, but if you can work toward the simplest way it removes superfluous code and makes it easier to understand, debug and maintain.
 
Joined
Feb 11, 2008
Messages
537 (0.15/day)
Likes
164
Location
Lincoln, Nebraska, USA
System Name This PC
Processor Intel Core i7 2600K @ 4.8 GHz
Motherboard ASUS P8Z68 Deluxe
Cooling Custom Water Cooling - CPU Only
Memory 8GB (2 x 4GB) AData DDR3
Video Card(s) GTX 1080 TI - EVGA FTW3 - Slave BIOS
Storage Corsair Force GT 128GB / Samsung 850 Pro 512 GB
Display(s) AOC AGON AG271QG - 165 Hz - AHVA
Case Dimastech v2.5 Easy
Audio Device(s) Onboard
Power Supply Antec 1.2kW HCP
Mouse Logitech G403 - Wired
Keyboard Rosewill - MX Browns
Software Windows 10 Pro x64
#7
You can also do

for(elementNumber = 0; elementNumber < arraySize; elementNumber++)
{
if((elementNumber + 1) % 10 == 0)
{
cout << list[elementNumber] << endl;
}

else
{
cout << list[elementNumber] << " ";
}
}