-1

I am a self learner, and I was going through a free class online. I am trying to put the values of the everyOther into an array, so I can access it to later. I have looked around the internet, but am not able to find anything resourceful. Can you show me how to store the output values of everyOther in to an array. Thanks in advance.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

long countDigit(long long n);

int main(void)
{
    long n;

   //This is asking for the input
    do
    {
        n = get_long("Number: ");
    }
    while(!(countDigit(n)>13));

    //Checksum math
    long everyOther = 0;

    while(n > 0)
    {
        long lastNumber = n/10;
        everyOther = lastNumber % 10;
        n = n / 100;
        printf("%li\n", everyOther);
    }

}

//This function helps us with the counting of the number
long countDigit(long long n) {
  return floor(log10(n) + 1);
}
  • 3
    Careful when tagging both C and C++. They are different languages. They both root in the C of the 1980s, but both have diverged from that common ancestor. – user4581301 Jan 22 '21 at 20:43
  • Declare an array, declare an index initialised to 0, store `everyOther` into the array at the current index and then increment index. Which of those steps do you have difficulty with? I find it very difficult to believe that you can't find any examples of storing values into an array. – kaylum Jan 22 '21 at 20:45
  • @kaylum how do I store everyOther into the array at the current index? – Samjohns081998 Jan 22 '21 at 20:51
  • `my_array[index] = everyOther;` – kaylum Jan 22 '21 at 20:53
  • @kaylum is this how to do it. `long storingArr[10];` `long index = 0;` `storingArr[index] = everyOther;` `index++;` – Samjohns081998 Jan 22 '21 at 20:57
  • @Samjohns081998 Pretty much – Asteroids With Wings Jan 22 '21 at 20:58
  • 3
    You should learn C++ from a good book, ideally. It covers basics like arrays! – Asteroids With Wings Jan 22 '21 at 20:59
  • Self learning is aided greatly by having [a few reference materials and language texts](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) around to consult. The Internet is a hive of scum and villainy, inundated with all manner of falsehoods, so until you know enough of the language to recognize the signatures of competent code and learn how to differentiate it from incompetent code, you are at the mercy of luck. Did Google point you at a credible tutorial or not? You have no way of knowing. – user4581301 Jan 22 '21 at 21:01
  • Remember the greatest advantage and greatest downside of arrays: They have a fixed size. If you make an array of 10 elements, `n` must never be allowed to be 10 or greater. Only 0 though 9 can be used as valid indexes. – user4581301 Jan 22 '21 at 21:04
  • @user4581301 ok that makes sense now. – Samjohns081998 Jan 22 '21 at 21:08
  • @gamusren i am doing it in c, and the array method that you described, i am doing 'storing[i] = everyOther;' with the counter increment, it is still not storing them. – Samjohns081998 Jan 22 '21 at 21:24
  • @gamusren, when I try to print the array, this is the what it shows, Element[1] = 0 Element[2] = 0 Element[3] = 0 Element[4] = 0 Element[5] = 0 – Samjohns081998 Jan 22 '21 at 21:27

3 Answers3

0

C++ array, once created with certain length, cannot be expanded or shrank. This is an advantage when it comes to maximizing the space efficiency, but more than often poses a restraint on your code.

In your case, since you are taking a user input with an unpredicted length (>13), you risk creating an array without enough space.

You have two options:

  1. Make a rule on the user's input and create array accordingly, for example, if you make sure that the there are maximum of 20 digits, then the array length could not exceed 20 with your

    long lastNumber = n/10; everyOther = lastNumber % 10;code.

What you can do to put everyother to array is to first create an array with length e.g, 20, by long foo [20] then transfer the everyother into the array each time with foo[i] = everyother remember to keep a counter on i to iterate to the next cell.

  1. Instead of array, use an vector, which is basically dynamic array that the system take care of the space restraint for you. This is not the most efficient but is fool-proof. To use this, #include <vector> first, then simply declare an vector with vector<long> foo; then add everyother with foo.push_back(everyother) on each iteration of the while loop.

To reuse both of these method, simply call foo[index_of_position] to extract the everyother information stored.

Remember that every datatype you try to store in these structures has to match the type they are created with (most of the time). e.g, a vector<int> would not accept a string object.

yaozhang
  • 69
  • 7
  • i am doing it in c, and the array method, i am doing the increment 'storing[i] = everyOther;' with the counter, it is not storing them. – Samjohns081998 Jan 22 '21 at 21:23
  • @Samjohns081998 can you elaborate a little bit more? C language syntax is a little different from C++, which may not apply the same exact code I wrote here. Can you try testing in C++? or can you tell me what error it gives you? Compile error? Have you created an array first? – yaozhang Jan 22 '21 at 21:25
  • when I try to print the array, this is the what it shows, Element[1] = 0 Element[2] = 0 Element[3] = 0 Element[4] = 0 Element[5] = 0 – Samjohns081998 Jan 22 '21 at 21:27
  • @Samjohns081998 seems like you created an array but did not store them. It could be a problem where you put the code in the wrong place. Can you try `foo[i] = everyother` immediately after `everyOther = lastNumber % 10;`? substitute i for iteration of course. With a for loop or whatever. – yaozhang Jan 22 '21 at 21:32
0

If I were you I would #include <vector> at the top, then declare std::vector<long> vectorArray;. You can then put the values into the vector, which is a dynamically sized array. It should be said though that this is a C++ feature, and will not work in C, but since this post is tagged with both I am assuming it will be fine.

// #include <cs50.h>
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <vector>

long countDigit(long long n);

int main(void)
{
    long n = 0; // initialise n

    //This is asking for the input
    do
    {
        std::cin >> n; // using this as input
    }
    while(!(countDigit(n)>13));

    //Checksum math
    long everyOther = 0;

    std::vector<long> vectorArray;
    
    while(n > 0)
    {
        long lastNumber = n/10;
        everyOther = lastNumber % 10;
        vectorArray.push_back( everyOther ); // this appends everyOther to the end of the array
        n = n / 100;
        printf("%li\n", everyOther);
    }

    for ( auto x : vectorArray )
        std::cout << x << ' ';      // sample printout of vector's contents
    
}

//This function helps us with the counting of the number
long countDigit(long long n) {
  return floor(log10(n) + 1);
}

camball
  • 43
  • 6
0

Using std::vector:

#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <vector>

long countDigit(long long n);

int main(void) {
  long n;

  // This is asking for the input
  do {
    n = get_long("Number: ");
  } while (!(countDigit(n) > 13));

  // Checksum math
  long everyOther = 0;

  std::vector<long> destination;

  while (n > 0) {
    long lastNumber = n / 10;
    everyOther = lastNumber % 10;
    n = n / 100;
    printf("%li\n", everyOther);

    destination.push_back(everyOther);
  }
}

// This function helps us with the counting of the number
long countDigit(long long n) { return floor(log10(n) + 1); }

As you are iterating over the digits of a random number, it is hard to use std::array efficiently, as it has a size set at compile-time. You could however make an array of a gigantic size, and use it as a buffer.

#include <array>
#include <cs50.h>
#include <math.h>
#include <stdio.h>

#define BUFFERSIZE 65536

long countDigit(long long n);

int main(void) {
  long n;

  // This is asking for the input
  do {
    n = get_long("Number: ");
  } while (!(countDigit(n) > 13));

  // Checksum math
  long everyOther = 0;

  std::array<long, BUFFERSIZE> destination;

  size_t i = 0;
  while (n > 0) {
    long lastNumber = n / 10;
    everyOther = lastNumber % 10;
    n = n / 100;
    printf("%li\n", everyOther);

    destination[i++] = everyOther;
  }
}

// This function helps us with the counting of the number
long countDigit(long long n) { return floor(log10(n) + 1); }
spoutnik
  • 149
  • 1
  • 9