【问题标题】:How to fill a 2D array in C according to their values?如何根据值填充 C 中的二维数组?
【发布时间】:2015-10-16 11:56:24
【问题描述】:

编辑:在 print_tables 函数中,我有相应地填充所有值的 for 循环。有什么问题是我试图计算每个元素的值,但是那里填满了错误的输入。 vars elastic 和 numOfDimples 是正确的,但是对于 var 距离及其计算是错误的。对于计算

            dimpleFactor = 120 - numOfDimples;
            distance = elasticity * (800 - (dimpleFactor * dimpleFactor));

它是将弹性的最终结果相乘,而不是相应地相乘。例如:

假设弹性 = 0.14 和 numOfDimples = 102。 所以当for循环第一次被执行时,会发生什么 弹性 = 1.12(弹性的最后一个值),所以这几乎会破坏方程。

这是完整的代码,任何修复它的建议都将非常感激。就像我应该使用结构或联合(因为我对 C 来说真的很陌生)。

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

const float MIN = 0.0;
const float MAX_ELAS = 1.0;
const float MAX_DIFFERENCE = 10.0;


void get_data(float *minDimples, float *maxDimples, float *elasticity);
void print_tables(float minDimples, float maxDimples, float elasticity);

void get_data(float *minDimples, float *maxDimples, float *elasticity)
{
    float minMaxDifference;
    bool minBool, maxBool, elasBool = false;
    minBool = maxBool = elasBool; //set booleans to false

    /* Prompt the user for data using a do-while loop
    * Exit the loop when data is valid
    */
    do {
        /* Get MINIMUM number of dimples data */
        printf("Enter the minimum number of dimples:\n");
        scanf("%f", minDimples);
        /* Check if it's valid */
        if (*minDimples < MIN) {
            //invalid data entered
            minBool = false;
        }
        else {
            minBool = true;
        }

        /* Get MAXIMUM number of dimples data */
        printf("Enter the maximum number of dimples:\n");
        scanf("%f", maxDimples);
        /* Check if it's valid */
        if (*maxDimples < MIN || *maxDimples < *minDimples) {
            maxBool = false;
        }
        else {
            maxBool = true;
        }

        minMaxDifference = *maxDimples - *minDimples;
        if (minMaxDifference > 10) {
            minBool = false;
        }

        /* Get elasticity step size data */
        printf("Enter the elasticity step size:\n");
        scanf("%f", elasticity);
        /* Check if it's valid */
        if (*elasticity < MIN || *elasticity > MAX_ELAS) {
            elasBool = false;
        }
        else {
            elasBool = true;
        }


        /* Print error message if something is invalid */
        if (elasBool == false || minBool == false || maxBool == false) {
            printf("Sorry, but your data is invalid\n");

        }
        else {
            /* Do nothing */
            printf("");
        }

    } while (elasBool == false || minBool == false || maxBool == false);


}


void print_tables(float minDimples, float maxDimples, float elasticity)
{
    float elasticityLength, minMaxDifference, setElasticity, dimpleFactor, distance, numOfDimples;
    float table[10][10]; //elasticity step size's and distance's (rows cannot be longer than 10 & columns cannot be > 100)
    int  rows, columns, i, j;

    setElasticity = elasticity;
    numOfDimples = minDimples;


    elasticityLength = MAX_ELAS / elasticity; //How many times does elasticity step size go into 1? /Columns
    minMaxDifference = maxDimples - minDimples; //How many times should rows go along? /Rows


    /* This loop is for filling the table 2D array */
    for (rows = 0; rows <= minMaxDifference; rows++) {
        elasticity = setElasticity;
        for (columns = 0; columns < elasticityLength; columns++) {

            /* Setting the dimple size's. table[0][i] */
            if (columns == 0 && rows >= 1) {
                printf("%.0f\t", numOfDimples);
                table[columns][rows] = numOfDimples;
                if (numOfDimples <= maxDimples) {
                    numOfDimples++;
                }
            }

            if (rows != 0 && columns != 0) {
                dimpleFactor = 120 - numOfDimples;
                distance = elasticity * (800 - (dimpleFactor * dimpleFactor));
//  <<< THIS calculation is wrong
                printf("%.1f\t", distance);
                table[columns][rows] = distance;
            }

            /* Setting the elasticity along column 1. table[i][0] */
            if (rows == 0 && columns >= 1) {
                printf("%.02f\n", elasticity);
                table[columns][rows] = elasticity;
                elasticity += setElasticity;
            }
        }
    }
    printf("\n-------------------\n");
    /* Print the table */
    for (i = 0; i <= minMaxDifference; i++) {
        for (j = 0; j < elasticityLength; j++) {
            printf("%0.2f\t", table[i][j]);
        }
        printf("\n");
    }



}


int main(int argc, char *argv[])
{

    float minDimples, maxDimples, elasticity;


    get_data(&minDimples, &maxDimples, &elasticity);
    print_tables(minDimples, maxDimples, elasticity);

    return 0;
}

我尝试将列循环放在外面,但仍然没有运气。我希望它看起来有点像 http://www.homeandlearn.co.uk/powerpoint/powerpoint_p3s21.html (距离具有正确的值)。

任何建议或解决方案将不胜感激!还使用适合此的二维数组吗?

【问题讨论】:

  • 我建议将columnsrows重命名为columnrow,现在看起来它们是总数而不是当前索引。
  • 当您将elasticityLengthminDimplesmaxDimplesminMaxDifference 与数组的整数索引一起使用时,为什么它们是浮点数?
  • @Arkku 是的,我知道这一点,但目前对此并不真正感兴趣。只想要一张工作台。虽然
  • @user2878551 但是您已经将数组硬编码为 10×10,并且无法检查 elasticityLengthminMaxDifference 的计算是否不超过该值。
  • @ameyCU 如果我没有 setElasticity 并且只有弹性。然后我必须做弹性+=弹性。并且每次都会加倍,而不是加上设定值。例如:(弹性 += 弹性)1 += 1;等于 2。2+= 2;等于 4。4 += 4;等于 8。现在如果我改用 setElasticity。 1 += 1;等于 2。2+= 1;等于 3. 等等。

标签: c arrays for-loop multidimensional-array


【解决方案1】:

至于代码结构,鉴于整个二维数组仅在print_tables 的本地,并且唯一的目的是打印数组,您不妨完全删除数组并直接打印值而不是存储它们暂时只是为了重新遍历打印它们的数组。

我还会将“标题”行和列与数据行分开处理;这通过删除ifs 简化了代码,因此更容易跟踪正在发生的事情。例如:

#include <stdio.h>
#include <math.h>

const static float MAX_ELAS = 1.0;

void print_tables(unsigned minDimples, unsigned maxDimples, float minElasticity) {
    const unsigned elasticityLength = (unsigned) ceilf(MAX_ELAS / minElasticity);
    float elasticity = minElasticity;

    // Header row
    (void) printf(" "); // Empty (0,0) cell
    for (unsigned col = 0; col < elasticityLength; ++col) {
        (void) printf("\t%.02f", elasticity);
        elasticity += minElasticity;
    }

    for (unsigned dimples = minDimples; dimples <= maxDimples; ++dimples) {
        float dimpleMultiplier = 120.0f - dimples;
        dimpleMultiplier = 800.0f - (dimpleMultiplier * dimpleMultiplier);
        elasticity = minElasticity;

        (void) printf("\n%u", dimples); // Header column
        for (unsigned col = 0; col < elasticityLength; ++col) {
            float distance = elasticity * dimpleMultiplier;
            (void) printf("\t%.1f", distance);
            elasticity += minElasticity;
        }
    }
    (void) printf("\n"); // Terminate last row
}

int main (void) {
    // TODO: Read input instead of hard-coded values (or use command-line?)
    print_tables(121, 125, 0.14);
    return 0;
}

输出:

         0.14    0.28    0.42    0.56    0.70    0.84    0.98    1.12
121     111.9   223.7   335.6   447.4   559.3   671.2   783.0   894.9
122     111.4   222.9   334.3   445.8   557.2   668.6   780.1   891.5
123     110.7   221.5   332.2   443.0   553.7   664.4   775.2   885.9
124     109.8   219.5   329.3   439.0   548.8   658.6   768.3   878.1
125     108.5   217.0   325.5   434.0   542.5   651.0   759.5   868.0

作为进一步清理的建议,也许指定elasticityStep,这是列之间elasticity 的增量,并分隔minElasticitymaxElasticity 而不是elasticityLength。那么列循环就像:

for (elasticity = minElasticity; elasticity <= maxElasticity; elasticity += step)

【讨论】:

  • @Arrku 我添加了整个代码。有更好的描述。非常感谢您的回复:D
  • @user2878551 你试过我建议的改变吗?如果这不是您要查找的内容,那么您如何为elasticity 提供一行手动计算的所需值。
  • @Arrku 当然我尝试了你的建议。但是它不起作用:'(我该如何手动执行?您的意思是在没有for循环的情况下执行吗?
  • @user2878551 我的意思是将输入值作为数字给出并使用例如计算器(或铅笔和纸=)来产生一行正确的结果,因为仍然不清楚代码应该是什么做。
  • @arrku 你又看了吗?认为它会更清楚:(。我希望用户输入值。所以它可以更改为用户的愿望。
猜你喜欢
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2018-01-27
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多