12 Replies - 2926 Views - Last Post: 24 June 2009 - 10:34 PM Rate Topic: -----

#1 babygirl(TM)   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-March 09

counting integers in an array

Posted 24 June 2009 - 09:34 AM

I have to make a program where I'm translating an integer into its individual digits and place each digit inside an array of size 10. I will then print the contents of the array and the number of digits in the original integer.
The problem I have is theres suppose to be a certain amount of Os in front of the number when the final output is made.

Here's what the output is suppose to look like:


Please enter a positive number (no more than 10 digits): 12345

The number you entered has 5 digits.

Big Number: 0000012345


here's my code:
#include <iostream>

using namespace std;

/* Author: Dominique Pope
   Class: IS 2043
   Date: June 25, 2009
   Purpose: To make a program where the user can enter a number, and the program
   will mathematically count how many digits the number is. 
*/

int main()
{
  // declare variables
  const long SIZE = 10;
  long num[SIZE]={0,0,0,0,0,0,0,0,0,0};
  
  // Ask for a number from user and store it.
  for(long i = 0; i < SIZE; i++)
  {	  
		 cout<<"Please enter a positive number (no more than 10 digits): " << endl;
		 cin >> num[i];
		 cout << "The number you entered has " << num[i]%10 << " digits." << endl;
		 cout << "Big Number: "<< num[SIZE] <<endl;
  }
  
  if(num[SIZE] < 0|| num[SIZE] > SIZE + 1)
  {
			   cout << "Invalid number! Try again." << endl;
			   return 0;
  }
  // Individually count the number of digits 
	 
  
  // The individually retrievable digits are placed inside the array. 
   system("PAUSE");
   return 0;
}

/* Output of program should be:
		  
		  Please enter a positive number (no more than 10 digits): 12345

		  The number you entered has 5 digits.

		  Big Number: 0000012345
*/




all i need is for someone to explain how i can get those zeros in...

Is This A Good Question/Topic? 0
  • +

Replies To: counting integers in an array

#2 computerfox   User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: counting integers in an array

Posted 24 June 2009 - 09:40 AM

you can have an if statement that checks if it's less than 10 and if it is then it will probably go through a for loop until it reaches 10. hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: counting integers in an array

Posted 24 June 2009 - 10:13 AM

View Postbabygirl(TM), on 24 Jun, 2009 - 11:34 AM, said:

I'm translating an integer into its individual digits and place each digit inside an array of size 10

So sorry! I misread the question and gave you code for an entirely different problem. All my program would do is count the number of digits entered, not place them in an array. I did not help any more than computerfox did.

However, in case you ever want to count how many characters are in a string:
#include <iostream>
#include<iomanip>
#include<string>
using namespace std;

int main()
{
  // declare variables
  const long SIZE = 10;
  string num = "";
  // Ask for a number from user and store it. //this will run ten times
  for(int i = 0; i < SIZE; i++)
  {       
          cout<<"Please enter a positive number (no more than 10 digits): " << endl;
          cin >> num;
          if(num.length() > SIZE)
          {
               cout << "Invalid number! Try again." << endl;
          }
          else
          {
              cout << "The number you entered has " << num.length() << " digits." << endl;
          }
  }
  
   system("PAUSE");
   return 0;
}


This post has been edited by OliveOyl3471: 24 June 2009 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#4 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1433
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: counting integers in an array

Posted 24 June 2009 - 10:31 AM

We don't usually give out free code but I can never pass up a bit of modulus magic :) (I even threw in some ternary!)

Chances are, you won't be able to hand this in to your professor anyway, since I doubt you've covered ternary properly yet and he'll be like "WHERE DID YOU STEAL TEH COEDZ FRM" so you'll have to use it as a reference to write your own anyway :P

The logic is all here. Modulus magic is always fun.

#include <iostream>

using namespace std;

void val_to_array(unsigned int to_convert, int array[]) {
    // use length of 10 to match INT_MAX - since we're dealing with ints,
    // it can't be greater than 2147483647*2 (unsigned int max)
    int index = 9; // start from the end
    to_convert *= 10; // initialise it, we'll divide instantly in our ternary fun
    while(index > 0) { // while we still have a digit
        // ZOMIGOSH TERNARY FUN!!1!ONE!
        array[index--] = ((to_convert = to_convert / 10) > 0) ? to_convert % 10 : 0;
    }
}

int main(int argc, char* argv[]) {
    unsigned int input = 0;
    cout << "Enter the value: ";
    cin >> input;

    int array[10] = {0};
    val_to_array(input, array); // array will be modified by the function
    for(int i = 0; i < 10; i++) {
        cout << array[i];
    }

    cin.get();
    return EXIT_SUCCESS;
}


Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#5 babygirl(TM)   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-March 09

Re: counting integers in an array

Posted 24 June 2009 - 10:38 AM

unfortunately i was gonna try both of those, but i can't use char. it has to be an int or long cuz he claims its "too easy" to use char or string to count them out... i'm probably gonna just turn it in how i have it cuz he said right now i'm at 30 pts outta 40...
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1433
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: counting integers in an array

Posted 24 June 2009 - 11:02 AM

Look again - mine doesn't use char or string. ;)

If you want to get the length, you could do something like this using the loop in main()
    for(int i = 0; i < 10; i++) {
        cout << array[i];
        if(length == 0 && array[i] != 0) {
            length = 10 - i;
        }
    }
    cout << endl << "The number of digits in the number is: " << length;

This post has been edited by gabehabe: 24 June 2009 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: counting integers in an array

Posted 24 June 2009 - 11:46 AM

to_convert *= 10; // initialise it, we'll divide instantly in our ternary fun

sorry gabe, aren't you risking an overflow here, meaning your code will only work up to 9 digits?
Was This Post Helpful? 0
  • +
  • -

#8 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1433
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: counting integers in an array

Posted 24 June 2009 - 12:13 PM

yeahhh, my bad ;)

void val_to_array(unsigned int to_convert, int array[]) {
    // use length of 10 to match INT_MAX - since we're dealing with ints,
    // it can't be greater than 2147483647*2 (unsigned int max)
    int index = 9; // start from the end
    while(index >= 0) { // while we still have a digit
        // ZOMIGOSH TERNARY FUN!!1!ONE!
        array[index--] = ((to_convert = (index == 8 ? to_convert : to_convert / 10)) > 0) ? to_convert % 10 : 0;
    }
}
That oughtta fix it.

This post has been edited by gabehabe: 24 June 2009 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#9 babygirl(TM)   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-March 09

Re: counting integers in an array

Posted 24 June 2009 - 12:29 PM

thanks for ya'lls help...

it works perfect now... the only thing is its still not outputting the zeros in the front or at least how i did it

heres my new code:

int main()
{
	double num, temp, *a;
	int digit=0, numzero, x, i;
	double big[10];
	
	cout << "Enter positive number (no more than 10 digits):" << endl;
	cin >> num;
	
	temp = num;

	while (temp>=1) // finds number of digits
	{
		temp = temp/10;
		digit++;
	}
	
	cout << "The number u entered has " << digit << " digits" << endl;
	   
	system("pause");
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#10 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: counting integers in an array

Posted 24 June 2009 - 12:50 PM

This will output the zeros in the front, and it uses an array. But it still uses string. :(

#include <iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
  // declare variables
  const long SIZE = 10;
  char numArray[SIZE]={'0','0','0','0','0','0','0','0','0','0'};
  string num = "";
  // Ask for a number from user and store it.
  
          cout<<"Please enter a positive number (no more than 10 digits): " << endl;
          cin >> num;
          if(num.length() > SIZE)
          {
               cout << "Invalid number! Try again." << endl;
          }
          else
          {
              cout << "The number you entered has " << num.length() << " digits." << endl;
          }
          int counter = num.length();
          for(int i = 0; i < num.length(); i++)
          { 
            numArray[SIZE - counter]= num.at(i); //fill array with our string 
            counter--;
          } 
          cout << "Big Number: ";
  for(int i = 0; i < SIZE; i++)
  { 
          cout << numArray[i];    
  }
  cout << endl;
   
   system("PAUSE");
   return 0;
}

/* Output of program should be:
          
          Please enter a positive number (no more than 10 digits): 12345

          The number you entered has 5 digits.

          Big Number: 0000012345
*/



This post has been edited by OliveOyl3471: 24 June 2009 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#11 babygirl(TM)   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-March 09

Re: counting integers in an array

Posted 24 June 2009 - 08:53 PM

I figured it out finally but i'm still having some errors when it comes to my if statement
here's the code:
#include <iostream>
#include <iomanip>

using namespace std;

/* Author: Dominique Pope
   Class: IS 2043
   Date: June 25, 2009
   Purpose: To make a program where the user can enter a number, and the 
   program will mathematically count how many digits the number is. 
*/

int main()
{
  // declare variables
  const int SIZE = 10;
  int temp,
	  digit = 0;
  int num[SIZE];
  
  // Ask for a number from user and store it.
  
  cout << "Please enter positive number (no more than 10 digits) :" << endl;
  cin >> num[SIZE];
  
  temp = num[SIZE];
  
  // Counts the number of digits 
  while (temp>=1)
	{
		temp = temp/10;
		digit++;
	}
  
  // tells the user how many digits they entered
  if(num[SIZE] <= SIZE)
/* right here it keeps on jumping straight to "Invalid number" & then it counts how many digits it has... i'm not understanding how my logic is wrong
*/
  {
		cout << "The number of digits in the number is: " << digit << endl;
  }
  // if user has entered 11 or more digits, an error occurs
  else 
  {
		cout << "Invalid number! Try again" << endl;
  }

   cout << "Big Number: " << setw(10) << setfill('0') << num[SIZE] << endl;
   system("PAUSE");
   return 0;
}



This post has been edited by babygirl(TM): 24 June 2009 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#12 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: counting integers in an array

Posted 24 June 2009 - 10:20 PM

View Postbabygirl(TM), on 24 Jun, 2009 - 10:53 PM, said:

I figured it out finally


Yes! That is awesome! I wish I had thought of this.

cout << "Big Number: " << setw(10) << setfill('0') << num[SIZE] << endl; 


But I see your problem here.

Check and see what you are actually putting into your array.
Add the line I added and run the code again, and you should be able to see why it's doing that.

int main()
{
[code]//your code here...
  
  // Ask for a number from user and store it.
  
  cout << "Please enter positive number (no more than 10 digits) :" << endl;
  cin >> num[SIZE];
  
  temp = num[SIZE];
  //I added this line so you could check what's in your array--your number is stored in the last place, num[10] (which is also num[SIZE]
  cout<<num[0]<<endl<<num[1]<<endl<<num[2]<<endl<<num[10];

//your code here...
  
  // tells the user how many digits they entered
  if(num[SIZE] <= SIZE) //this does not work unless they enter a single digit, since num[SIZE] is equal to the entire number they input

}



This post has been edited by OliveOyl3471: 24 June 2009 - 10:22 PM

Was This Post Helpful? 0
  • +
  • -

#13 babygirl(TM)   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-March 09

Re: counting integers in an array

Posted 24 June 2009 - 10:34 PM

View PostOliveOyl3471, on 24 Jun, 2009 - 09:20 PM, said:

View Postbabygirl(TM), on 24 Jun, 2009 - 10:53 PM, said:

I figured it out finally


Yes! That is awesome! I wish I had thought of this.

cout << "Big Number: " << setw(10) << setfill('0') << num[SIZE] << endl; 


But I see your problem here.

Check and see what you are actually putting into your array.
Add the line I added and run the code again, and you should be able to see why it's doing that.

int main()
{
[code]//your code here...
  
  // Ask for a number from user and store it.
  
  cout << "Please enter positive number (no more than 10 digits) :" << endl;
  cin >> num[SIZE];
  
  temp = num[SIZE];
  //I added this line so you could check what's in your array--your number is stored in the last place, num[10] (which is also num[SIZE]
  cout<<num[0]<<endl<<num[1]<<endl<<num[2]<<endl<<num[10];

//your code here...
  
  // tells the user how many digits they entered
  if(num[SIZE] <= SIZE) //this does not work unless they enter a single digit, since num[SIZE] is equal to the entire number they input

}





thank you so much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1