Cannot print the whole int array

190 Views Asked by At

My output : 6 123456 44 0 67 87 0

What I want is this output: 10 123456 44 0 67 87 0 0 45 55 88

How come my program didn't print all the integer? Can anyone please help me to check what is wrong with my code? What changes should I make to my code? Here is my code.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
main()
{
        char m[100][1000]={"123456","44","","67","87","","","45","55","88"};
        int len;
        int i;
        int j;
        int k;
        int n;
        int numm[1000];
        n=0;
        len=strlen(m[n++]);
        printf("%d\n",len);
        for (i=0;i<len;i++)
            numm[i]=atoi(m[i]);
        for (j=0;j<len;j++)
            printf("%d\n",numm[j]);
        for (k=1;k<len;k++)
        {
            if (numm[k]>100||numm[k]<0)
            {   
                printf("Error. Marks out of range.\n");
                exit(0);
            }
        }   
        return 0;   
}
6

There are 6 best solutions below

0
On

With a quick look i would say it's because you are using the same length (len) on both dimension

and your dimensions are differents

4
On
len=strlen(m[n++]); // m[0]
printf("%d\n",len);

Prints length of string "123456" = 6

[EDIT]

How to get length of array

    len = 0;

    for (i = 0; i < 100; i++) {
        if (m && m[i][0] == '\0') {
            break;
        }
        len++;
    }
    printf("%d\n",len);

[NOTE] This works provided you are using "0" and not "" to print 0

0
On

Well, if you want that to be output, do it!

Currently, you take the length of the first string, "123456", and abuse it as the length of the array, which is completely unrelated.

The size of the array is computed as sizeof m / sizeof m[0], with sizeof m being the total size in bytes and sizeof m[0] the size of the first element.

But then you'd print 1000 elements. But you only want to print the declared ones.

As a char[100][1000], this seems quite hard to me; you'd better work with a char * m[1000].

But you could do

len = sizeof m / sizeof m[0]; // initial length
while (!m[--len][0]); // decrement len until you find "something".
0
On

your m array is a 2D array.

m[0]="123456" |  m[0][0]='1'   m[0][1]='2'  ...
m[1]="44"     |  m[0][0]='4'   m[0][1]='4'
m[2]=""       |  m[0][0]='\0'
m[3]="67"

The

int n=0; len=strlen(m[n++]);

is equivalent to

int n=0; len=strlen(m[0]); n=n+1;

so strlen() here will return then length of the first strin in the array (m[0]) and not the number of element of your m array.

0
On

My recomendation is that you should try to debug your code and then line by line see where something its wrong. However most probably your problem is in the index lengths. In case of every iteration you're still using the same lenght of the string len=strlen(m[n++]); which is of course not true for the rest of them.

Try start with first one:

len = sizeof m / sizeof m[0];

and then look through the table using first index in a loop for example like in the answer of glglgl:

while (m[--len][0]);
0
On
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char m[100][1000]={"123456","44","","67","87","","","45","55","88"};
    int len;
    int i,j,k,n;
    int numm[1000];

    for(len = 100 ; len >0; --len)
        if(*m[len-1]) break;
    printf("%d\n",len);
    for (i=0;i<len;i++)
        numm[i]=atoi(m[i]);
    for (j=0;j<len;j++)
        printf("%d\n",numm[j]);
    for (k=1;k<len;k++){
        if (numm[k]>100||numm[k]<0){   
            printf("Error. Marks out of range.\n");
            exit(0);
        }
    }   
    return 0;
}