Does not print numeric digits

Asked

Viewed 89 times

0

1 - Crie um programa em linguagem C, que tenha os seguintes itens (0.5 pontos).
a) Armazenar em uma constante do tipo caractere, o número do seu RA sem o traço.
b) Criar um vetor do tipo inteiro com o tamanho da *string* armazenada na sua constante.
c) Alimentar o vetor de forma que cada caractere do seu RA, que está na constante, fique armazenado em uma posição do seu novo vetor. Para isso, utilize um laço de repetição.
d) Imprimir o vetor preenchido de forma que apareça um número em cada linha. Para isso, utilize um laço de repetição diferente do utilizado na alternativa anterior.

My code is this:

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

int main()
{

     const char *RA = "19480685";
     int vetor[strlen(RA)];

     for (int i = 0; i < strlen(RA); i++) vetor[i]=RA[i];

     for (int i = 0; i < strlen(RA); i++) printf ("%d\n", vetor[i]);



    return 0;
}

But the result of the program comes out with these random numbers:

resultado

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

You’re sending print number (%d), and want to print characters (%c), this is the mistake. Also I saved the size of the string in a variable because the function strlen() is extremely slow and should not be used, given in this case, but the statement asked to do so. The statement is ambiguous in some points.

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

int main() {
    const char *RA = "19480685";
    int tamanho = strlen(RA);    
    int vetor[tamanho];
    for (int i = 0; i < tamanho; i++) vetor[i] = RA[i];
    for (int i = 0; i < tamanho; i++) printf ("%c\n", vetor[i]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • instead of "for", as I use "while" to print this vector?

  • That is not in question, I answered what you asked. And it is not necessary to do this.

  • is q in the question ta telling that has q use 2 different loops, and I only used the for now :/

  • Actually you used 3, one invisible and two visible, I don’t know why you’re saying you used 1.

  • of the ties (for, while, while), of which I am speaking

  • That’s not what’s in the statement, he just wants two different ties, doesn’t want you to use one while.

  • the teacher said he wanted to use 2 different types of repetition loops in alternatives C, D

  • It is not in the statement, you are with problems of interpretation of text and anyway it does not make sense to do it.

  • I have an error, when I run your code in dev c++ gives an error. ([Error] 'for' loop initial declarations are only allowed in C99 or C11 mode) ([Error] redefinition of 'i') ([Error] 'for' loop initial declarations are only allowed in C99 or C11 mode) Before it was in C++, and there was no error, and now ta in C what can be?

  • Don’t use Dev-C++ it is notoriously very bad, but you have to set the build to C99 or C11 as the error message does, but this problem is something else, not part of the question you asked here. Every problem must be a different question.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.