• 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.03/day)
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
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,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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
607 (0.10/day)
Location
Omaha, Nebraska, USA
System Name Built By Me
Processor Intel Core i9 9900K @ 5.1 GHz
Motherboard Gigabyte Z390 Aorus Ultra
Cooling Custom Water Cooling - CPU Only
Memory 32GB (2 x 16) GSkill Ripjaws V DDR4
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 1TB Samsung 970 Evo NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro
Power Supply 850W Seasonic Platinum
Mouse Razer Viper V2 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
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 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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.03/day)
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
Awesome, that's exactly what I needed.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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
607 (0.10/day)
Location
Omaha, Nebraska, USA
System Name Built By Me
Processor Intel Core i9 9900K @ 5.1 GHz
Motherboard Gigabyte Z390 Aorus Ultra
Cooling Custom Water Cooling - CPU Only
Memory 32GB (2 x 16) GSkill Ripjaws V DDR4
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 1TB Samsung 970 Evo NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro
Power Supply 850W Seasonic Platinum
Mouse Razer Viper V2 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
You can also do

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

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