落書きノート

ふと自分が気になった事を書いてます

慣習 C++で問題を解く

#include <cstdio>
#include <iostream>

using namespace std;

int main(void){
  int a[5];

  for(int i = sizeof(a) / sizeof(a[0]),j = 0; i > 0; i--,j++){
    a[i] = i;
    //printf("a[%d] = %d\n", j, a[i]);
    cout << "a[" << j << "] = " << a[i] << endl;
  }

  return 0;
}

#include <iostream>

using namespace std;

int main(void){
  double a[5];

  for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++){
    a[i] = 0.0;
    cout << "a[" << i << "] = " << a[i] << endl;
  }

  return 0;
}

#include <iostream>

using namespace std;

int main(void){
  double a[] = {0.0,0.0,0.0,0.0,0.0};

  for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++){
    cout << "a[" << i << "] = " << a[i] << endl;
  }

  return 0;
}

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(void){
  int a[6];

  srand(time(NULL));

  for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++){
    a[i] = 1 + rand() % 10;
    cout << "a[" << i << "] = " << a[i] << endl;
  }

  return 0;
}

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(void){
  int a[6];
  int i = 0, chk = 0, pre = 0;

  srand(time(NULL));

  while(true){
    chk = 1 + rand() % 10;

    if(i >= sizeof(a) / sizeof(a[0])){
      break;
    }else if(chk != pre){
      pre = chk;
      a[i] = chk;
      cout << "a[" << i << "] = " << a[i] << endl;
      i++;
    }else{
      continue;
    }
  }

  return 0;
}