Freddy Rivas
Freddy Rivas

Reputation: 13

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!

Upvotes: 0

Views: 177

Answers (7)

Akshay Rao
Akshay Rao

Reputation: 342

#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.

Upvotes: 0

bhargavi kotapati
bhargavi kotapati

Reputation: 1

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;
 }

Upvotes: 0

Yazan
Yazan

Reputation: 78

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;
}

Upvotes: 0

Richard
Richard

Reputation: 122

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 K.I.S.S.

Upvotes: 0

merlin2011
merlin2011

Reputation: 75565

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;
}

Upvotes: 2

Rosme
Rosme

Reputation: 1049

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;
}

Upvotes: 1

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

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;    
} 

Upvotes: 0

Related Questions