如何在C中打印2D数组

时间:2014-02-09 22:58:29

标签: c arrays

我一直试图让我的程序打印条形图。

问题出在底部,我在这里创建一个2D数组来保存值然后尝试 打印数组。问题是什么都不打印。我试图解决它几个小时没有运气。有什么建议吗?

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

#define DELIM " " /* the delimiter */
#define MAX_CHANGE (10.0/86400.0)       /* 10kg/day */

    /* seconds in a day is 24 hours * 60 minutes * 60 seconds */
    /* return 0 if the passed strings don't math, 1 otherwise */

/* defines the structure of Node */
struct Node{
    char * id;
    float weight;
    int time;
    int count;
    struct Node * next;
} *head, *p, *t, *last;

/* Constructor which returns a pointer to a new node*/
struct Node *newNode(int *time, char * id, float *w)
{   /*note malloc returns a pointer */
    struct Node *r = (struct Node *)malloc( sizeof(struct Node) );
    r->time = *time;
    r->id = strdup(id); //a duplicate id is made to prevent all the nodes from using the same userID
    r->weight = *w;
    r->count = 1;
    r->next = NULL;
    return r;   
}

/* prints the list starting with head */
printList(struct Node * head)
{
    while(head != NULL)
    {
        printf("%d %s %f\n",head->time,head->id,head->weight);
        head = head->next;
    }

    return 0;
}


int main() {

    char line[1024];    
    int lasttime = 0;
    int success;
    int timestamp;
    int duration;
    char userID[1000] = "";
    char *token;
    char temp[1000];
    float weight;
    float lastweight;
    float change;
    float changePerTime;

    head = (struct Node*)malloc(sizeof(struct Node));
    head->id = "";
    head->weight = 0.0;
    head->time = 0;
    head->next = NULL;

    last = head;

    /*FILE * f = fopen("C:\\Users\\Chris\\Documents\\School\\York\\Computer Science\\2031 Software Tools\\Labs\\lab3\\testcases\\01.in","r");  */

    /*  last points to the last node in the list
        head is always the same node
        p is used to travers the list
        t is a pointer the most recent occurrense of a user record
    */

    while (fgets(line,1024,stdin) != NULL) {
        userID[0] ='\0'; // resets userID
        token = strtok(line, DELIM);
        success = sscanf(token,"%d",&timestamp);

        if (success < 1 || timestamp == 0)
        {
            printf("Invalid time\n");
            continue;
        }

        while((token = strtok(NULL,DELIM) ) != NULL && token[0] != '.' && ! isdigit(token[0]) )
        {   
            strcpy(temp,token); // 
            strcat(temp,DELIM ); // adds space between each token
            strcat(userID, temp); // src temp must be a const string, not a pointer
            temp[0] = '\0';
        }

        userID[strlen(userID)-1] = '\0'; //erases the tailing space.

        if(strlen(userID) > 179 || !strlen(userID) )
            {printf("Illegal userID\n"); continue; } 
        else if(token == NULL || sscanf(token,"%f", &weight) < 1 || weight < 30.0 || weight > 300.0)
            {printf("Illegal weight\n"); continue; }
        else if (lasttime >= timestamp)
            {printf("Nonmonotonic timestamps\n"); continue; }
        else {
            /* sets t to last found user record and sets "last" to the last record*/
            for(p = head, t = NULL; p != NULL; p = p->next)
            {   
                if(strcmp(userID,p->id) == 0)
                {
                    t=p;    
                }

                last = p; // set last to last p.
            }

            if(t == NULL)
            {
                printf("OK newuser\n");
            }           
            else if(t != NULL)
            {
                /* increments count of id's for this user */
                (t->count)++; 
                duration = timestamp - t->time;
                change = weight - t->weight;
                changePerTime = change / duration;
                if(changePerTime < -MAX_CHANGE || changePerTime > MAX_CHANGE)
                    printf("Suspiciously large weight change\n");
                else
                    printf("OK\n");
            }

            /* adds node to end of list */
            last->next = newNode(&timestamp,userID,&weight);
            last = last->next;

            /* adds time to last time */
            lasttime = timestamp;
            }       
        }

    //fclose(f);
            char bc[10][last->count];

            int j, i, k, bh;
            for(p = head; p != NULL, j <= last->count; p=p->next)
            {
                if(strcmp(last->id,p->id) == 0)
                {
                    for(i = 11, k=0, bh = (int)(p->weight / 30);i >= 0; i--)
                    {
                        if(k < bh) 
                        {
                            bc[i][j] = '*'; 
                            k++;
                        }
                        else bc[i][j] = ' ';
                    }
                j++;
                }                   
            }

//printf("%c", bc[9][1]);

            int m=0, n=0;
            for(m < 10; m++;)
            {           
                for(n=0 ;n < last->count; n++)
                {           
                    printf("%c",bc[m][n]);
                }
                printf("%c",'\n');
            }
    }

1 个答案:

答案 0 :(得分:1)

您的外部for循环部件放置不正确。而不是:

for(m < 10; m++;)

你想:

for(m=0;m < 10; m++)

条件m<10是for循环的第二部分,而你错误地把它放在循环的初始化部分。同样,增量语句i++在您的条件部分,因此您没有增加m变量的发生。