0

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!

asked Oct 30, 2014 at 18:44
8
  • "if there is an easier way to initialize both" Depends on the number schemes you need to store in the arrays. Commented Oct 30, 2014 at 18:47
  • you may use loops to feed in the data Commented Oct 30, 2014 at 18:48
  • Can't you just loop to create the arrays? We are actually not sure why you created a restriction for yourself by trying to initialize the array during declaration Commented Oct 30, 2014 at 18:50
  • You don't even need arrays, really. Commented Oct 30, 2014 at 18:51
  • 2
    You have a really wide monitor. Commented Oct 30, 2014 at 18:51

7 Answers 7

2

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;
}
answered Oct 30, 2014 at 18:52
Sign up to request clarification or add additional context in comments.

1 Comment

The initial ftime for this one is a 9 instead of a 0.. so it starts as 09 01 02 03...
1

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;
}
answered Oct 30, 2014 at 18:53

Comments

0

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; 
} 
answered Oct 30, 2014 at 18:51

Comments

0

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.

answered Oct 30, 2014 at 18:56

Comments

0

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;
}
answered Oct 30, 2014 at 18:58

Comments

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

Comments

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.

answered Oct 30, 2014 at 18:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.