I’m doing a c job for college in case anyone can help

Asked

Viewed 114 times

0

inserir a descrição da imagem aqui

I made my code normally, but when it comes to testing it right for some and wrong for others, besides varying the answer depending on the compiler if you think the error would help a lot.

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

void main() {
  int n, i = 0, re, soma, k = 0;

  do {
    printf("informe quantos numeros tem na fileira: ");
    scanf("%d", & n);

    if (n <= 0 || n > 1000000) printf("valor invalido!\n\n informe outro:\n");

  } while (n < 0 || n > 1000000);

  int numeros[n];

  for (i = 0; i < n; i++) {
    do {
      printf("informe os numeros(de 1 a 1000): ");
      scanf("%d", & numeros[i]);

      if ((numeros[i] < 0 || numeros[i] > 1000)) printf("valor invalido!\n\n informe outro:\n");

    } while (numeros[i] < 0 || numeros[i] > 1000);

  }

  for (i = 0; i < n; i++) {
    soma = 0;

    for (k = 0; k < n; k++) {

      soma = soma + numeros[i + k];

      if (soma % 8 == 0) {
        //printf("\n%d\n", soma);
        re++;
      }
    }
  }

  printf("\n\n%d", re);
}
  • 2

    Read https://pt.meta.stackoverflow.com/questions/8388/que-erro-cometi-ao-formular-minha-pergunta?cb=1 and see if there’s anything you can do to improve your question.

  • 2

    Hello Vitor, already debug line by line to identify the problem? The debug is part of learning and helps to better understand how the program works, I suggest doing this, there will probably solve or reach a point that can not, and it will be easier to ask for help for example "gave error or invalid value in line XXX in the YYY command" :)

2 answers

3


I don’t program in C, but I decided to test your code online, I only made two changes: the first, I put the initial value of re to 0, just like you did with the variables i and k. It was starting at 32000 and a broken.

The second modification was in the loop k, whose initial value I changed to i, being like this:

for (i = 0; i < n; i++) {
    soma = 0;

    for (k = i; k < n; k++) {
        soma = soma + numeros[k];

        if (soma % 8 == 0) {
            re++;
        }
    }
}

The reason for this change is that the first loop determines the initial value of the rectangle and the second, the end of it. In fact, starting the second loop at index 0, you will be repeating calculation that you have already done.

Before

inserir a descrição da imagem aqui

Afterward

inserir a descrição da imagem aqui

Being i = 0 and k = 1, he will add 3 + 4. If i = 1 and k = 0, it will add up again 3 + 4.

I tested these examples there on two sites and gave ok (I have no compiler installed).

Here: https://onlinegdb.com/YjZpwUPyD

  • "gave ok", that means you actually found 3 solutions for 3 4 6 0 2 9? What would they be? I can’t see this solution

  • @arfneto Yes, found the 3. In the statement only says to return only to amount solutions. However, it follows the modified code which also shows solutions: https://onlinegdb.com/KpUIPjAKi

  • Did you find WHAT 3? I even wrote a program, but I can’t see the 3 solutions for these numbers. Neither can my program. What solution your program found?

  • @arfneto did not see the code of the link I posted in the previous comment? Anyway, follow the results: 3 + 4 + 6 + 0 + 2 + 9, 6 + 0 + 2 and 0.

  • I understand. No, I had not seen it. I never see links from comments. Thank you. I did not consider 0 as a solution. my bad

  • corrected my example. ! Thanks

  • Sure, it helped a lot!

Show 2 more comments

2

Regarding the example with 3 4 6 0 2 9 it is clear that 6 0 2 add up to 8 and it is ok, the sum of all numbers gives 24 so this is the second solution. A third solution is 0 by itself, as would be the case for any multiple of 8 in a square. (@Kiritonito pointed this out to me)

A general notation

A possible solution can be written as a pair (i,j) with the start in i and the length j of the sequence.

To 8 0 8 0 8 we have

0, 1    0, 2    0, 3    0, 4    0, 5
1, 1    1, 2    1, 3    1, 4    2, 1
2, 2    2, 3    3, 1    3, 2    4, 1

As 15 solutions.

Particular case

  • With all squares with multiples of 8 the total solutions should be (N+1)*N / 2, and 10 squares with 8 should lead to 55 solutions

An example in C

I changed a little the original program to record on file Q8.txt the solutions found, so you can check the program and see the "squares"found.

#define LIMITE_ (1000 * 1000)

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

int main(int argc, char** argv)
{
    const char* s_padrao = "q8.txt"; // entrada padrao
    unsigned    limite = LIMITE_; // limite padrao
    char        arquivo_s[80];
    char        na_tela = 0;
    char linha[256];
    if (argc > 1) 
        strcpy(arquivo_s, argv[1]);
    else
        strcpy(arquivo_s, s_padrao);
    //fprintf(stderr, "Saida em \"%s\"\n", arquivo_s);

    FILE* S = fopen(arquivo_s, "a");
    if (S == 0) return -1; // nao abriu saida

    unsigned    N;
    int         res = scanf("%d", &N);
    fgets(linha, 256, stdin); // le o resto da linha
    if (res != 1) return -2;
    if (N > LIMITE_) N = LIMITE_;
    fprintf(S,"%d numeros\n", N);
    short* V = (short*)malloc(sizeof(short) * N);
    if (V == NULL) return -3;

    // le os numeros ate preencher o vetor inicial
    for (unsigned i = 0; i < N; i += 1)
    {
        res = scanf("%hd", (V+i) );
        if (res != 1)
            return -4;
        else
            fprintf(S, "%6d", *(V + i));
    }
    fprintf(S, "\n");

    // busca as solucoes
    unsigned total = 0;
    for (unsigned i = 0; i < N; i += 1)
    {
        if (*(V+i) % 8 == 0)
        {
            fprintf(S, "%d, 1\n", i);
            total += 1;
        }
        unsigned soma = *(V+i); // o primeiro, claro
        for (unsigned j = i+1; j < N; j += 1)
        {
            soma += *(V + j);
            if ((soma % 8) == 0)
            {
                fprintf(S, "%d, %d\n", i, 1+j-i);
                total += 1;
            };
        }
    }
    fprintf(S, "Total: %d solucoes\n\n", total);
    fclose(S);
    printf("%d\n", total);
    free(V);
    return 0;
};  // main()

Exit in Q8.txt (examples)

This is the generated file

6 numeros
     3     4     6     0     2     9
0, 6
2, 3
3, 1
Total: 3 solucoes

7 numeros
     1     1     1     1     1     1     1
Total: 0 solucoes

5 numeros
     8     0     8     0     8
0, 1
0, 2
0, 3
0, 4
0, 5
1, 1
1, 2
1, 3
1, 4
2, 1
2, 2
2, 3
3, 1
3, 2
4, 1
Total: 15 solucoes


4 numeros
     0     0     0     0
0, 1
0, 2
0, 3
0, 4
1, 1
1, 2
1, 3
2, 1
2, 2
3, 1
Total: 10 solucoes

4 numeros
     8    16    24    32
0, 1
0, 2
0, 3
0, 4
1, 1
1, 2
1, 3
2, 1
2, 2
3, 1
Total: 10 solucoes

exit at the terminal

6
3 4 6 0 2 9
3

7
1 1 1 1
1 1 1
0

5
8 0 8 0 8
15

Back to the original program

  if (n <= 0 || n > 1000000) printf("valor invalido!\n\n informe outro:\n");

  } while (n < 0 || n > 1000000);

  int numeros[n];

  for (i = 0; i < n; i++) {
    do {
      printf("informe os numeros(de 1 a 1000): ");
      scanf("%d", & numeros[i]);

      if ((numeros[i] < 0 || numeros[i] > 1000)) printf("valor invalido!\n\n informe outro:\n");

    } while (numeros[i] < 0 || numeros[i] > 1000);`

Suggestions

  • declare control variables of for in charge itself
  • when using do/while avoid constructions like this where you test the same thing twice
  • main() returns int.
  • declare constant dimension vectors or use malloc() and create the vector on time.

Browser other questions tagged

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