如何根据它们的值在C中填充2D数组?

时间:2015-07-26 04:53:14

标签: c arrays for-loop multidimensional-array

编辑:在print_tables函数中,我有for循环,相应地填充所有值。它有什么问题,我正在尝试计算每个元素的值,但是输入错误。 vars弹性和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 (距离具有正确的值)。

任何建议或解决方案将不胜感激!还使用适合此的2D阵列?

1 个答案:

答案 0 :(得分:2)

至于代码结构,假设整个2D数组只是print_tables的本地,并且唯一的目的是打印数组,你也可以完全删除数组并直接打印值而不是临时存储它们只是为了重新遍历阵列打印它们。

我还与数据行分开处理“标题”行和列;这会通过删除if来简化代码,因此可以更轻松地跟踪正在进行的操作。例如:

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