Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Vibhor16

  • 2 years ago

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?

  • This Question is Closed
  1. ndani14
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 5

    have you ever used java or python? what so the need to 100 digit number for?

  2. Vibhor16
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    C++ assignment in school.

  3. ndani14
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 5

    ok, are you wanting to do operations on it like addition, multiplication, etc ?

  4. Vibhor16
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yup

  5. ndani14
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 5

    well to answer your current question you could do something like this. at the moment i'm wasting a lot of space though #include <iostream> #include <string.h> #include <stdlib.h> 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"); }

  6. Vibhor16
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thx

  7. marguedas
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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 <string.h> #include<stdlib.h> #include<iostream> 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; } ~

  8. shoot.me
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    try using exponential form....

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

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.