techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Dec 3, 2009, 09:32 PM   #1
Lampmaster
75 Posts
 
Lampmaster's Avatar
 
Join Date: Aug 2009
Location: Washington The State
Posts: 149 (0.11/day)
Thanks: 35
Thanked 17 Times in 14 Posts

System Specs

More noob C++ programmer help

Hey guys, one last problem here.

I have this function for outputting an array

Quote:

// ===================
// 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?
Lampmaster is offline  
Reply With Quote
Old Dec 3, 2009, 09:51 PM   #2
Aleksander
2000 Posts
 
Aleksander's Avatar
 
Join Date: Dec 2009
Posts: 3,038 (2.35/day)
Thanks: 653
Thanked 283 Times in 229 Posts

System Specs

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
Aleksander is offline  
Reply With Quote
Old Dec 3, 2009, 10:44 PM   #3
dcf-joe
200 Posts
 
dcf-joe's Avatar
 
Join Date: Feb 2008
Location: Nebraska, USA
Posts: 327 (0.17/day)
Thanks: 61
Thanked 18 Times in 18 Posts

System Specs

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.
dcf-joe is offline  
Reply With Quote
The Following User Says Thank You to dcf-joe For This Useful Post:
Old Dec 3, 2009, 11:10 PM   #4
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,254 (5.27/day)
Thanks: 591
Thanked 5,510 Times in 2,948 Posts

System Specs

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;
    }
}
Kreij is offline  
Reply With Quote
The Following User Says Thank You to Kreij For This Useful Post:
Old Dec 3, 2009, 11:35 PM   #5
Lampmaster
75 Posts
 
Lampmaster's Avatar
 
Join Date: Aug 2009
Location: Washington The State
Posts: 149 (0.11/day)
Thanks: 35
Thanked 17 Times in 14 Posts

System Specs

Awesome, that's exactly what I needed.
Lampmaster is offline  
Reply With Quote
Old Dec 4, 2009, 12:14 AM   #6
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,254 (5.27/day)
Thanks: 591
Thanked 5,510 Times in 2,948 Posts

System Specs

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.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Dec 4, 2009, 02:05 PM   #7
dcf-joe
200 Posts
 
dcf-joe's Avatar
 
Join Date: Feb 2008
Location: Nebraska, USA
Posts: 327 (0.17/day)
Thanks: 61
Thanked 18 Times in 18 Posts

System Specs

You can also do

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

else
{
cout << list[elementNumber] << " ";
}
}
dcf-joe is offline  
Reply With Quote
The Following User Says Thank You to dcf-joe For This Useful Post:
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob programmer help Lampmaster Programming & Webmastering 6 Nov 25, 2009 07:04 PM
Looking for a good Java Programmer Wastedslayer Programming & Webmastering 3 Apr 9, 2009 11:31 PM
Couple more quesions for a somewhat noob OC'er ;) Pepsiaddict Overclocking & Cooling 4 Nov 30, 2006 02:40 PM
im a noob at overclockin... help me! karolpl2004 Overclocking & Cooling 12 Dec 3, 2005 11:03 PM


All times are GMT. The time now is 02:07 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts