anonymous
  • anonymous
In C++,How can I initialize a big number into array? For Example if a want to assign 123456789 into a array num[8] then can I write num[8]=1,2,3,4,5,6,7,8,9; or will I have to write num[0]=1; num[1]=2; num[2]=3; num[3]=4; num[4]=5; num[5]=6; num[6]=7; num[7]=8; num[8]=9; It is a 100 digit number.Is there any other alternative?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
schrodinger
  • schrodinger
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
have you ever used java or python? what so the need to 100 digit number for?
anonymous
  • anonymous
C++ assignment in school.
anonymous
  • anonymous
ok, are you wanting to do operations on it like addition, multiplication, etc ?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
yup
anonymous
  • anonymous
well to answer your current question you could do something like this. at the moment i'm wasting a lot of space though #include #include #include int main() { char* number = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; int len = strlen(number); printf("Digits: %d\n", len); int digits[100]; char tempNum[2]; tempNum[1] = 0; for(int i = 0; i != len; i++) { tempNum[0] = number[i]; digits[i] = atoi(tempNum); } for(int i = 0; i != len; i++) { printf("%d", digits[i]); } printf("\n"); }
anonymous
  • anonymous
thx
anonymous
  • anonymous
The answer that was given has a few issues: - It cannot handle numbers with more than 100 digits. Buffer overflow occurs in that case. - Even though the 'atoi' conversion in the main for loop is correct, there may be another way of achieving the conversion without using an extra buffer, and this is done by using the ASCII code of each char. I would implement the solution as follows: #include #include #include int main() { char* number = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; int len = strlen(number); printf("Digits: %d\n", len); //allocating memory char *digits = new char[len]; for(int i = 0; i < len; i++) { digits[i] = number[i] - '0'; } for(int i = 0; i != len; i++) { printf("%d", digits[i]); } printf("\n"); //free up the memory delete digits; } ~
anonymous
  • anonymous
try using exponential form....

Looking for something else?

Not the answer you are looking for? Search for more explanations.