Home > front end >  I Want to add the numbers in the matrix
I Want to add the numbers in the matrix

Time:08-01

I want the sum of the numbers in the matrix in each column to add up, tried doing this using different variation's of the same music[i 1][j] =music[i][j], but its not working, so basically what the program does, it assigns 3 points to the first number the user inputs as his favorite song, the second song get 2 points and the third 1 point, i want the matrix at the end to sum up all the points from the participants and give me the total.

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

int main()

{   int num=0, pers=0;

    int i,j, k;

    int votos=0;

    printf("Digite la cantidad de personas:");

    scanf("%d", &pers);

    float music[pers 1][10];

    for(i=0;i<pers 1;i  ){

       for(j=0;j<10;j  ){
        music[i][j]=0;
       }
    }

for(i=0;i<pers;i  ){

  printf("Participante %d :\n",i 1);

       for(k=1;k<=3;k  ){

         printf("Digite el numero de sus 3 canciones favoritas %d:\n",k);

          scanf("%d",&num);

           for(j=0;j<9;j  ){
             if (k==1){
              music[i][num-1]=3;
            }
               if (k==2){
                music[i][num-1]=2;
              }
               if (k==3){

                music[i][num-1]=1;
              }

               music[10][j] =music[i][j];
       }
    }
}
    for(i=0;i<pers 1;i  ){
       for(j=0;j<10;j  ){
        printf("%.2f\t",music[i][j]);
       }
       printf("\n");
    }
        return 0;
     }

CodePudding user response:

GDB is your friend for these sorts of problems. You can install an interactive debugger and step through your code line by line to see what values your matrix takes on certain input.

If you did that, you may have saw that you're accessing the 10th row of your matrix. Even if pers is initialized to three.

There's also an issue where you begin summing music values as they're being initialized (leading to over counting).

  for (i = 0; i < pers; i  )
  {
    printf("Participante %d :\n", i   1);
    for (k = 1; k <= 3; k  )
    {
      printf("Digite el numero de sus 3 canciones favoritas %d:\n", k);
      scanf("%d", &num);
      if (k == 1)
      {
        music[i][num - 1] = 3;
      }
      if (k == 2)
      {
        music[i][num - 1] = 2;
      }
      if (k == 3)
      {
        music[i][num - 1] = 1;
      }
    }
  }

  for(j = 0 ; j < 10; j  )
  {
    for (i = 0; i < pers 1; i  )
        music[pers][j]  = music[i][j];
  }

I'm hoping this code doesn't fully solve your problem since it'll be good for you to look into gdb and learn how to interact with your code and understand where its deviating from your expectations.

It'll also help to do as paddy suggested and explain what your input and output is supposed to look like.

CodePudding user response:

Try This Code It working and sum upResult

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

int main(){ 
int num=0, pers=0;

int i,j, k;

int votos=0;

printf("Digite la cantidad de personas:");

scanf("%d", &pers);

float music[100][100];
float sum[100][100];

for(i=0;i<pers 1;i  ){

   for(j=0;j<10;j  ){
    music[i][j]=0;
   }
}

for(i=0;i<pers;i  ){

printf("Participante %d :\n",i 1);

   for(k=1;k<=3;k  ){

     printf("Digite el numero de sus 3 canciones favoritas %d:\n",k);

      scanf("%d",&num);

       for(j=0;j<9;j  ){
         if (k==1){
          music[i][num-1]=3;
        }
           if (k==2){
            music[i][num-1]=2;
          }
           if (k==3){

            music[i][num-1]=1;
          }

           sum[i][j] =music[i][j];
       }
    }
}
for(i=0;i<pers 1;i  ){
   for(j=0;j<10;j  ){
    printf("%.2f\t",sum[i][j]);
   }
   printf("\n");
}
    return 0;
 }
  •  Tags:  
  • c
  • Related