Question

I need to change an integer and put it into an array but in a way...

I need to change an integer and put it into an array but in a way that it is read correctly.

Arrays are typically drawn to be read left to right with the 0 element on the left and the largest on the right. However, arrays are a completely made up concept and are not physical in nature. So you can draw them and think about them anyway you want. For this problem having the right side as the 0 element and the left side as the largest makes much more sense.
Take the example of the number 299,793. We show how it is stored in the array below. The 3 is in the one's position, the 9 in the 10's position and so on. This neatly corresponds to the index of the array. The addition and multiple algorithms given below use this representation.

bigint

Index: n ... 7 6 5 4 3 2 1 0
Place: 10^n's ... 10^7's 10^6's 10^5's 10000's 1000's 100's 10's 1's
Value: 0 ... 0 0 2 9 9 7 9 3

now i have a constructor bigint::bigint(int); and I know i need to peel off each digit from the int and place it into the appropriate location in the array of int. To do this you need to use integer division and mod.

int digit, 
    num = 128;
digit = num % 10;  // num mod 10 is 8 in this case.
num = num / 10;    // num div 10 is 12.
digit = num % 10;  // digit is now 2
num = num /10;     // num is now 1
digit = num % 10;  // digit is now 1
num = num /10;     // num is now 0

So you can build a loop that goes until num == 0 and peel off each digit, putting them in the array.

my question is how would i build this loop to place them correctly in the array?

Homework Answers

Answer #1

CODE IN C++:

#include <iostream>

using namespace std;
int digitCount(int number){
int count = 0 ;
while(number!=0){
number = number / 10 ;
count +=1 ;
}
return count;
}
int main()
{
int number;
cout<<"Enter an integer:";
cin>>number;
int count = digitCount(number);
int arr[count];
int i;
int temp = number;
for(i=count-1;i>=0;i--){
arr[i] = temp%10;
temp = temp / 10;
}
cout<<"Below is your number in the array form:"<<endl;
for(i=0;i<count;i++)
cout<<arr[i]<<" ";

return 0;
}
OUTPUT:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
One way to represent a very large integer (one that won't fit into a variable of...
One way to represent a very large integer (one that won't fit into a variable of type short, int, or even long) is to use an array. The array is of type int, so each element in the array can hold an integer -- we will store just one digit of our number per array element. So if a user entered 2375, it might be stored as -------------------------- | 2 | 3 | 7 | 5 | ... | --------------------------...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
A programmer that you work with, Peter, is a jerk. He is responsible for an array...
A programmer that you work with, Peter, is a jerk. He is responsible for an array that is a key part of an important program and he maintains a sum of the array value.He won't give you access to this array; however, your boss has told you that you need to get input from the user and then place it into the array.Each evening Peter will scan the code and remove any illegal references to his array. Using pointers, access...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    //...
Complete the java code as per the comments public class Sorting {    ///////////////////////////////////////////////    // STEP 1 -- Make sorting methods generic    ///////////////////////////////////////////////       /**    * Re-orders the contents given array using the insertion sort algorithm.    *    * @param data The array to be sorted.    */    //TODO: Make me generic to work on any kind of Comparable data!    public static void insertionSort(int[] data)    {        int insert; // temporary...
What output do we get? How do we get that output? Consider the following method :...
What output do we get? How do we get that output? Consider the following method : public static void processArray (int [] array) { int x = 0; for (int i = 0; i < array.length - 1; i++ ) { if (array [i] < array [i+1]) { System.out.println (i + 1) ;         }     } } In the left-hand column below are specific arrays of integers. Indicate in the right- hand column what value would be printed by...
Microsoft Visual Studio in C#: I have this code and need to loop through each element...
Microsoft Visual Studio in C#: I have this code and need to loop through each element of the patient's array and add to a listbox (lbAccountDisplay) each property of the patient's object. This is the 2nd time to ask this question; it should have all the detail you need to help me. It is a piece of a project and I am trying not to give you the whole project. How do I use a foreach loop to loop through...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...