在C中形成数组和打印列/行

时间:2016-03-18 00:26:10

标签: c arrays random

我有一个家庭作业,要求我为我建造的一个相信气象站获得25个随机整数(rand())。我不知道我到目前为止所做的事情是否正确,我也不知道从哪里开始。

我不想要答案,我想了解如何将代码放在一起。我在谷歌和我的教科书中搜索过,但对我来说没有意义。我甚至不确定我是否可以在这里发布这类问题,但我想我至少会尝试。

#include <stdio.h>
#include <stdlib.h>

int main ()
{


int get_value()
{
    int ran_num=rand()%40+60;
    return ran_num;
}

int temp_value[25];
int ran_num=0;
for (ran_num=0; ran_num<sizeof(temp_value); ran_num++)
{
    temp_value[0] = ran_num;
}
return 0;
}

4 个答案:

答案 0 :(得分:0)

使用printf进行一些基本格式化,即

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

  int main(void) {
    int i = 0;
    printf("c1 c2 c3 c4 c5 c6 c7 c8 c9 c0\n");
      for(i = 0; i < 10; i++) {
      printf("%0.2d %0.2d %0.2d %0.2d %0.2d %0.2d %0.2d %0.2d %0.2d %0.2d\n",
          i,  
          i + 1,  
          i + 2,  
          i + 3,
          i + 4,
          i + 5,
          i + 6,
          i + 7,
          i + 8,
          i + 9 
          );  
    }   
  }

打印

c1 c2 c3 c4 c5 c6 c7 c8 c9 c0
00 01 02 03 04 05 06 07 08 09
01 02 03 04 05 06 07 08 09 10
02 03 04 05 06 07 08 09 10 11
03 04 05 06 07 08 09 10 11 12
04 05 06 07 08 09 10 11 12 13
05 06 07 08 09 10 11 12 13 14
06 07 08 09 10 11 12 13 14 15
07 08 09 10 11 12 13 14 15 16
08 09 10 11 12 13 14 15 16 17
09 10 11 12 13 14 15 16 17 18

答案 1 :(得分:0)

#include <stdio.h> 
#include <stdlib.h> 

int main ()
{
    int temp_value[25];
    for (int i=0; i<sizeof(temp_value); i++){
        temp_value[i] = rand()%40+60;
        printf("%d \r\n", temp_value[i]);
    }
    return 0;
}

答案 2 :(得分:0)

  1. 你应该单独写函数。
  2. 一个功能做一件事。
  3. 想想你要做什么,把目标分成小程序。
  4. #include <stdio.h>
    #include <stdlib.h>
    
    int get_value()
    {
        int ran_num=rand()%40+60;
        return ran_num;
    }
    
    int main ()
    {
    
        int temp_value[25];
        int ran_num=0;
        for (ran_num=0; ran_num<sizeof(temp_value); ran_num++) {
            temp_value[0] = ran_num;
        }
        return 0;
    }
    

答案 3 :(得分:0)

这是七个步骤中的修复过程。希望这会给你一些见解。这正是我为自己的代码所做的事情。

// original

#include <stdio.h>
#include <stdlib.h>

int
main()
{

    // BUG: this is a "nested" function, which does _not_ exist in C
    int get_value() {
        int ran_num = rand() % 40 + 60;

        return ran_num;
    }

    int temp_value[25];
    int ran_num = 0;

    for (ran_num = 0; ran_num < sizeof(temp_value); ran_num++) {
        temp_value[0] = ran_num;
    }

    return 0;
}
// moved function to proper level

#include <stdio.h>
#include <stdlib.h>

int get_value() {
    int ran_num = rand() % 40 + 60;

    return ran_num;
}

int
main()
{

    int temp_value[25];
    int ran_num = 0;

    // BUG: this _only_ sets all temp_value[0] = 0
    for (ran_num = 0; ran_num < sizeof(temp_value); ran_num++) {
        temp_value[0] = ran_num;
    }

    return 0;
}
// changed get_value variable name to make things more clear

#include <stdio.h>
#include <stdlib.h>

int get_value() {
    int val = rand() % 40 + 60;

    return val;
}

int
main()
{

    int temp_value[25];
    int ran_num = 0;

    // BUG: this _only_ sets all temp_value[0] = 0
    for (ran_num = 0; ran_num < sizeof(temp_value); ran_num++) {
        temp_value[0] = ran_num;
    }

    return 0;
}
// changed loop to call get_value

#include <stdio.h>
#include <stdlib.h>

// changed variable name
int get_value() {
    int val = rand() % 40 + 60;

    return val;
}

int
main()
{

    int temp_value[25];
    int ran_num = 0;

    // BUG: this _only_ sets all temp_value[0] to a random value
    for (ran_num = 0; ran_num < sizeof(temp_value); ran_num++) {
        temp_value[0] = get_value();
    }

    return 0;
}
// changed loop to set all values in array

#include <stdio.h>
#include <stdlib.h>

// changed variable name
int get_value() {
    int val = rand() % 40 + 60;

    return val;
}

int
main()
{

    int temp_value[25];
    int ran_num = 0;

    // this sets all values, but there is still a bug ...
    // it will overflow temp_value
    for (ran_num = 0; ran_num < sizeof(temp_value); ran_num++) {
        temp_value[ran_num] = get_value();
    }

    return 0;
}
// changed loop to set all values in array but _not_ overflow

#include <stdio.h>
#include <stdlib.h>

// changed variable name
int get_value() {
    int val = rand() % 40 + 60;

    return val;
}

int
main()
{

    int temp_value[25];
    int ran_num = 0;

    // now corrected, this sets all values to a random value
    for (ran_num = 0; ran_num < sizeof(temp_value) / sizeof(temp_value[0]);
        ran_num++) {
        temp_value[ran_num] = get_value();
    }

    return 0;
}
// final cleanup

#include <stdio.h>
#include <stdlib.h>

// changed variable name
int get_value() {
    int val = rand() % 40 + 60;

    return val;
}

int
main()
{

    int temp_value[25];
    int temp_index;

    // now corrected, this sets all values to a random value
    for (temp_index = 0;
        temp_index < sizeof(temp_value) / sizeof(temp_value[0]);
        temp_index++) {
        temp_value[temp_index] = get_value();
    }

    return 0;
}