简体   繁体   中英

How to print a 2D array in C

I've been trying to get my program to print a barchart.

The issue is at the bottom, where I make a 2D array to hold the values and then attempt to print the array. The problem is that is prints nothing. I've tried to solve it for a few hours with no luck. Any suggestions?

#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');
            }
    }

Your outer for loop parts are incorrectly placed. Instead of:

for(m < 10; m++;)

You want:

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

The condition, m<10 , is the second part of the for loop, whereas you've mistakenly put it in the initialization part of the loop. Similarly, the increment statement, i++ , was in your condition part, so you had no incrementing of the m variable happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM