0

iI'm trying to understand how to properly use arrays in C and tried to write a simple program, which is supposed to take a 5-integers array and eliminate zeroes to the left. This is my attempt:

#include <stdio.h>

int main() {
    int seq[5];
    int i;
    int cor[5];
    int counter;
    printf("Type the 5 numbers: ");
    scanf("%s", &seq);
    for (i=0; i<5; i++){

        if (seq[i] != 0) {
            for (counter=0; counter<5-i; counter++){
                cor[counter]=seq[i+counter];
            }
            break;
        }
    }
    printf("%s", cor);
    return 0;
}

The idea was that when something such as 00101 was entered, the program would look at each entry and check whether it is 0. If it isn't, at the position i, it would write a new array in which the 0-th position is assigned the value of the original array at i, the 1-th position would be assigned the value at i+1 and so on, and then it would print this new array, which should have no useless zeroes to the left. But it just prints the original array. What is wrong? Sorry for the begginner's question.

4
  • 2
    This would be better asked on SO, I think. But note that you can't read in a string (%s) into an integer array. The contents of the string are not automatically converted to integers. Commented Nov 12, 2014 at 11:07
  • 1
    Your inner loop starts with counter = 0 again, also you use j which should not be defined. counter should start at the current value of i. Say in your example the first 1 is at position 2, so you want to start copying from there. Commented Nov 12, 2014 at 11:08
  • Thank you Thorsten, but it seems that doesn't solve the issue. And I'd be even more confused if it did, shouldn't "counter" start at 0 in order for it to write in the 0-th entry of "cor"? And sorry about the j, I was sure I had fixed that before pasting it here... Commented Nov 12, 2014 at 11:15
  • Well, it won't help you to understand arrays (so I won't post this as an answer), but the quickest, slickest way to do this is to read in a string and then call atoi() to convert it to an integer (or 0, if it is not a valid integer) and which will strip those leading zeros for you :-) tutorialspoint.com/c_standard_library/c_function_atoi.htm Commented Nov 12, 2014 at 14:42

2 Answers 2

1

You have two ways to do what you want.

  1. using chars :

    #include <stdio.h>
    
    int main() {
        char seq[6]; /* reserve one place for terminating null */
        int i;
        char cor[5];
        int counter;
        printf("Type the 5 digits : ");
        scanf("%5s", seq); /* limit to 5 chars and seq is already an array : ne & */
        for (i=0; i<5; i++){
            /* should control seq[i] >= '0' && seq[i] <= '9' */
            if (seq[i] != '0') {
                for (counter=0; counter<6-i; counter++){ /* also copy terminating null */
                    cor[counter]=seq[i+counter];
                }
                break;
            }
        }
        printf("%s", cor);
        return 0;
    }
    

    but user could give you chars that are not digits.

  2. using ints :

    #include <stdio.h>
    
    int main() {
        int seq[5];
        int i;
        int cor[5];
        int counter;
        printf("Type the 5 numbers: ");
        i = scanf("%d%d%d%d%d", seq, seq+1, seq+2, seq+3, seq+4); /* seq+i = &(seq[i]) */
        /* should control i == 5 */
        for (i=0; i<5; i++){
    
            if (seq[i] != 0) {
                for (counter=0; counter<5-i; counter++){
                    cor[counter]=seq[i+counter];
                }
                break;
            }
        }
        printf("%d%d%d%d%d", cor[0], cor[1], cor[2], cor[3], cor[4]);
        return 0;
    }
    
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot store a string in an integer array. You have to store strings in character arrays.

1 Comment

To elaborate: seq is an array of int. But the %s parameter to scanf tells it that you want a string, and a string is an array of char.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.