简体   繁体   中英

Initialize Array C++

I have the code to print out time in the following format: 00 01 02 03 04 05 06 07 08 09 10 11... only that the first number (0) needs to be above the second number (0).. Below is what I have

#include <iostream>

using namespace std;

void printArray (int arg[], int length) {
  for (int n=0; n<length; ++n)
    cout << arg[n] << ' ';
  cout << '\n';
}

int main() 
{
    int ftime[99] = {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2};
    int ttime[99] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};

    cout << "Time: ";printArray(ftime,21);
    cout << "     ";printArray(ttime, 21);

    return 0;
}

Now the following prints out:

Time: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2
      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0

Which is exactly what I want, but I need to do this all the way to 99 and was wondering if there is an easier way to initialize both the ftime and ttime arrays other than the way I did it. Any input would be greatly appreciated!

Just feed it with a loop.

#include <iostream>

using namespace std;

void printArray (int arg[], int length) {
  for (int n=0; n<length; ++n) {
      cout << arg[n] << ' ';
  }
  cout << '\n';
}

int main() 
{
    int ftime[100]; 
    int ttime[100]; 
    for (int i = 0; i < 100; i++) {
        ftime[i] = i / 10;
        ttime[i] = i % 10;
    }

    cout << "Time: "; 
    printArray(ftime,100);
    cout << "      "; 
    printArray(ttime,100);



    return 0;
}

Simple loop would do it.

int j = -1;
for(unsigned int i = 0; i < 99; ++i) {
    ttime[i] = i % 10;
    if(i % 10 == 0) {
        ++j;
    }
    ftime[i] = j;
}

something like this?

int ftime[99];
int i;
for (i=0; i < 99; i++) {
  if (i/2 == 1)
   ftime[i] = 1;
  else if (i==20)
   ftime[i] = 2;
  else
    ftime[i] = 0;    
} 
for (i=0; i < 99; i++) {
  if (i/2 < 2)
   ttime[i] = i % 10;
  else 
    ttime[i] = 0;    
} 
#define MAX 99

int ftime [MAX+1]; // Includes 00 too
int ttime [MAX+1]; // Includes 00 too

for (int ctr = 0; ctr < MAX+1, ctr++) {
    ftime[ctr] = floor(ctr/10);
    ttime[ctr] = ctr % 10; // modulus oper
}

This may have syntax errors, sorry. Didn't have a c-compiler handy on this computer of mine.

You could do a dual-layered for-statment example:

int k = 0;
for(int i = 0; i < 9, i++){
      for(int j = 0; j < 9, j++){
           ftime[k] = i;
           ttime[k] = j;
           k++;
      }
 }

Remember KISS

this should fix it

#include <iostream>

using namespace std;

void printArray (int arg[], int length) {
  for (int n=0; n<length; ++n)
    cout << arg[n] << ' ';
  cout << '\n';
}

int main() 
{
    int ftime[99]; 
    int ttime[99];

    for(int j=0; j < 99; j++)
    {

        ftime[j] = j/10;
        ttime[j] = j%10;
    }
    cout << "Time: ";printArray(ftime,99);
    cout << "      ";printArray(ttime, 99);

    return 0;
}

declare both as integers and use the following for loop

for(int i=0;i<100;i++);
 {
  ftime[i]=i/10;
  ttime[i]=i%10;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM