473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Possible use of counter in function?

sonic
40
Have a quick question dealing with a program I've been working on all day. The program meets all the requirements for class and has displayed proper output through various tests. What I want to know is if i could use a counter of some sort in my printPattern function to count the total number of characters printed in the pattern. I have the logic worked out in my printLastLine function, but what if I had a program that had the possibility of going up to 1400 instead of just 14? The way I've currently coded the program would be rather inefficient and time consuming for one to hard code in. Like I said, I'm just curious for my own knowledge as I've already emailed my program in to my instructor.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // Program allows user to select number and type of pattern to be printed to screen
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. // Function prototypes
  8.  
  9. void getInput(int&, char&);
  10. void printError(int&, char&);
  11. int printPattern(int, char);
  12. void printLastLine(int, char);
  13.  
  14. int main()
  15. {
  16.     int numRows = 0;
  17.     char typeChar = 0;
  18.  
  19.  
  20.     getInput(numRows, typeChar);
  21.     printPattern(numRows, typeChar);
  22.     printLastLine(numRows, typeChar);
  23.  
  24.  
  25.     return 0;
  26. }
  27.  
  28. //************************************************************************
  29. // Definition of getInput which obtains user info on how to build pattern*
  30. //************************************************************************
  31.  
  32. void getInput(int& rows, char& characterSelect)
  33. {
  34.  
  35.     cout << "How many rows do you want in the pattern (even numbers only: 2-14)? ";
  36.     cin >> rows;
  37.     cout << "What character do you want the pattern made of (select from: * + # or $)? ";
  38.     cin >> characterSelect;
  39.     printError(rows, characterSelect);
  40. }
  41.  
  42. //************************************************************************
  43. // Definition of printError which determines if user has entered         *
  44. // acceptable input.                                                     *
  45. //************************************************************************
  46.  
  47. void printError(int& rows, char& characterSelect)
  48. {
  49.  
  50.     if ((rows < 2) || (rows > 14) || (rows%2 != 0))
  51.     {
  52.         cout << "\nInvalid number of rows. Please retry.\n";
  53.         cout << "\n";
  54.         system("pause");
  55.         system("cls");
  56.         getInput(rows, characterSelect);
  57.     }
  58.     else if ((characterSelect != '*') && (characterSelect != '+') && (characterSelect != '#') && (characterSelect != '$'))
  59.     {
  60.         cout << "\nInvalid character. Please retry using one of the four characters given.\n";
  61.         cout << "\n";
  62.         system("pause");
  63.         system("cls");
  64.         getInput(rows, characterSelect);
  65.     }
  66.  
  67. }
  68.  
  69. //************************************************************************
  70. // Definition of printPattern displays pattern based on user entered data*
  71. // and tabulates the number of characters printed on screen.             *
  72. //************************************************************************
  73.  
  74. int printPattern(int rows, char characterSelect)
  75. {
  76.     int halfValue = rows / 2;
  77.  
  78. for (int row = 0; row <= halfValue; row++)
  79.     {    
  80.         for(int character = 0; character < row; character++)
  81.             cout << characterSelect;
  82.             cout << "\n";
  83.     }
  84.  
  85.     for (int row2 = halfValue; row2 >= 0; row2--)
  86.     {
  87.         for(int character2 = 0; character2 < row2; character2++)
  88.             cout << characterSelect;
  89.             cout << "\n";
  90.     }
  91.  
  92.     return rows;
  93. }    
  94.  
  95. //**********************************************************************
  96. // Definition of printLastLine function displays to the user the number*
  97. // of characters used to print the pattern as well as the type of      *
  98. // character used.                                                     *
  99. //**********************************************************************
  100.  
  101. void printLastLine(int numRows, char characterSelect)
  102. {
  103.     int totalCharacters = 0;
  104.  
  105.     if (numRows == 2)
  106.         totalCharacters = 2;
  107.     else if (numRows == 4)
  108.         totalCharacters = 6;
  109.     else if (numRows == 6)
  110.         totalCharacters = 12;
  111.     else if (numRows == 8)
  112.         totalCharacters = 20;
  113.     else if (numRows == 10)
  114.         totalCharacters = 30;
  115.     else if (numRows == 12)
  116.         totalCharacters = 42;
  117.     else if (numRows == 14)
  118.         totalCharacters = 56;
  119.  
  120.     cout << "The total number of " << characterSelect << "'s displayed was " << totalCharacters << ".\n";
  121.     cout << "\n";
  122. }
  123.  
  124.  
Thanks for any thoughts or ideas.
Dec 5 '06 #1
2 1204
horace1
1,510 Expert 1GB
try the following in the printLastLine() function
Expand|Select|Wrap|Line Numbers
  1.   int r=numRows/2;
  2.   cout << "total characters " << ((r*(r+1))) << endl;
  3.  
it should give you the number of total characters for any number of rows

it is a similar problem to calculating the storage requirements for a triangular matrix see
http://www.ncsa.uiuc.edu/UserInfo/Re...ml/essl43.html
Dec 5 '06 #2
sonic
40
Thank you for your input. The code worked like a charm. Now time for me to actually dig into how that formula actually works.
Dec 5 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Paul Rubin | last post by:
I'd like to have a function (or other callable object) that returns 0, 1, 2, etc. on repeated calls. That is: print f() # prints 0 print f() # prints 1 print f() # prints 2 # etc. ...
7
by: JellyON | last post by:
Hi. Is there a way to delay a call to a page counter (ie. call to a server script from an IMG tag) for the purpose to not lock the page loading awaiting counter be displayed. Maybe a...
0
by: Earl Anderson | last post by:
KB Article Q140908 provided the following function to create an Auto Incrementing Counter: Function Next_Custom_Counter () On Error GoTo Next_Custom_Counter_Err Dim MyDB As Database Dim...
24
by: Arne Demmers | last post by:
Hi, As a project of mine I have to do some C programming, which in fact isn't the real problem. The problem it self is that I will have to write some code that is as minimal as possible, and as...
4
by: dsimmons | last post by:
On my website http://seasidequilters.blogspot.com/ I'm having a problem that someone thinks might be associated with Firefox browsers. I'm hoping to find someone in this forum that might be...
9
by: Daniel Vukadinovic | last post by:
I want to implement an index/counter to my linked list. Why? I wrote a search function which searches the list for elements based on their values (say I add an element and I assign the value 54...
7
by: mistral | last post by:
I use htaccess to protect directory and granting access to download file only for the authorized users. Just want implement simple PHP file download counter for single file. I need track the number...
3
blackstormdragon
by: blackstormdragon | last post by:
Here were our instructions: "My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to...
3
by: tedpottel | last post by:
Hi I cannot get sessions to work. I have the following code </body> <? session_start(); $counter++; print "You have visited this page $counter times during this session";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.