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;
}
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.
Try This Code It working and sum up
#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;
}
Related
Here's my function that returns the Sum of all pair numbers in an array, and the Average of Odd numbers. Although it outputs the Average as zero for some reason.
#include <stdio.h>
int MoySom(int Tab[],float* Moyenne,int Length)
{
int S=0,C=0;
*Moyenne=0;
for(int i=0;i<Length;++i)
{
if(Tab[i] % 2 == 0)
{
S=S+Tab[i];
}
else if(Tab[i] % 2 != 0)
{
*Moyenne+=Tab[i];
++C;
}
}
*Moyenne=*Moyenne/C;
return S;
}
void main()
{
int Length,Tab[Length];
float Moyenne;
printf("Entrer la longeur de tableau: ");
scanf("%d",&Length);
for(int i=0;i<Length;++i)
{
printf("Entrer l'element %d: ",i);
scanf("%d",&Tab[i]);
}
printf("Somme est:%d\nMoyenne est: %.2f",
MoySom(Tab,&Moyenne,Length), Moyenne);
}
At least these problems:
Wrong declaration order
int Length,Tab[Length]; is junk. The declaration of Tab[Length] is happening, yet the value of Length is indeterminate.
Something more like the below. Declare Tab[] after Length is assigned.
int Length;
float Moyenne;
printf("Entrer la longeur de tableau: ");
scanf("%d",&Length);
int Tab[Length];
Better code checks the return value of scanf()
int cnt = scanf("%d",&Length);
if (cnt != 1 || Length <= 0) {
Report_Error_and_exit();
}
int Tab[Length];
Parameter evaluation order assumed
Calculate Moyenne, then used use it.
//printf("Somme est:%d\nMoyenne est: %.2f",
// MoySom(Tab,&Moyenne,Length), Moyenne);
printf("Somme est:%d\n", MoySom(Tab,&Moyenne,Length));
printf("Moyenne est: %.2f", Moyenne);
Potential /0
*Moyenne=*Moyenne/C; may attempt divide by zero. Better code would prevent that.
Unneeded test
if(Tab[i] % 2 == 0) {
S=S+Tab[i];
} else if(Tab[i] % 2 != 0) {
*Moyenne+=Tab[i];
simplifies to
if(Tab[i] % 2 == 0) {
S=S+Tab[i];
} else {
*Moyenne+=Tab[i];
i wrote this code that asks to the user to input how many numbers wants to input, then asks for the numbers and from this creates an array then:
call the function VerificaSequenza
checks that the sum of the first two is equal to the third one
if yes does this again with the second and the third equal to the forth and so on
returns back 1 if is true false if not
the Question is: why i can't even see on the screen the printf messages? it just finishes immediately without asking for numbers and without printing
#include<stdio.h>
int VerificaSequenza(int lunghezza, int sequenza[])
{
int ContoSomma = lunghezza - 1;
int casella1 = 0;
int casella2 = 1;
int risultato = 2;
int controllo;
int i;
int messaggio;
for(i=0; i<ContoSomma; i++)
controllo = sequenza[casella1] + sequenza[casella2];
++casella1;
++casella2;
if (controllo == sequenza[risultato])
{
messaggio = 1;
++risultato;
}
else
{
messaggio = 0;
return messaggio;
}
}
int main()
{
int lunghezza;
int sequenza[lunghezza];
printf("Da quanti numeri è composta la sequenza?");
scanf("%d", &lunghezza);
printf("scrivi la sequenza");
for(int i=0; i<lunghezza; i++)
scanf("%d", &sequenza[i]);
int messaggio = VerificaSequenza(lunghezza, sequenza);
printf("%d", messaggio);
}
I have tried to store a result in a vector and then to print all those stored values, I think I'm very far, but that's what I was able to develop until now, I'd like some help on what's wrong and how it could you do it, thank you and good night
#include <stdio.h>
int main(void) {
int i;
int x;
int vetor[]={};
int a, b;
int resultado;
printf("Quantos resultados esse vetor vai receber tem ? ");
scanf("%d", &x);
printf("\n");
for(i=1; i<=x; i++){
printf("Digite valores para serem somados e armazenados no vetor:\n");
scanf("%d%d", &a, &b);
resultado = a+b;
resultado = *vetor;
printf("vetor contém: %d\n", *vetor);
}
printf("\n");
printf("você conseguiu !!!");
return 0;
}
`````````````````````````````````````````````````````````````````´´´´
#include <stddef.h>
#include <stdio.h>
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define ARRAY_SSIZE(arr) ((ptrdiff_t)ARRAY_SIZE(arr))
int main(void)
{
ptrdiff_t x;
int a, b;
int resultado;
printf("Quantos resultados esse vetor vai receber tem ? ");
scanf("%ti", &x);
int vector[x] = {0};
printf("\n");
for (ptrdiff_t i = 0; i < ARRAY_SSIZE(vector); i++) {
printf("Digite valores para serem somados e armazenados no vetor:\n");
scanf("%d%d", &a, &b);
resultado = a + b;
vector[i] = resultado;
printf("vetor contém: %d\n", vetor[i]);
}
printf("\n");
printf("você conseguiu !!!\n");
return 0;
}
You should give the array (not vector) a size (x in this case) to create it.
Assignments (a = b;) work this way: the program calculates whatever is on the right hand side, and copies that value into the variable that is on the left hand side of the =.
When you use (access, write, ...) an array, you have to specify the index (position) of the array you want to use.
It is better to use sizeof to calculate the size of an array for loop limits so that if you change the declaration of the array, the code still works (see ARRAY_SIZE, and ARRAY_SSIZE which is a signed version, so that comparison is done between integers of the same sign, to avoid unexpected bugs).
Always end your last printf with a \n, so that it doesn't get mixed with other text in the terminal.
The program needs you to enter the number of lines and columns (m and n) , enter elements into the array, then specifiy an element to be found and find it.
Once you do , print the position found at (starting with 1) , the line and column index, also specify the number of times the element was found.
The problems with my program : it prints several times bad numbers before the final printf that shows the correct location. If the location of the searched is at the start it shows nothing. I tested with printf the second for loop and saw that it only displayed the last 2 elements entered into the array.
Another problem is that i get an error anytime i try to enter a array that is bigger than [2] [2] , for example [3][3].
#include <stdio.h>
#include <stdlib.h>
main()
{
int m=0,n=0,tablou[m][n],i,j,e,nr=0,poz=0;
printf("Introduceti nr. de linii si nr. de coloane");
scanf("%d %d",&m,&n);
printf("Introduceti elementele in tablou\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&tablou[i][j]);
}
}
printf("Introduceti elementul pe care vreti sa-l gasiti");
scanf("%d",&e);
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
poz++;
if(tablou[i][j]==e)
{
printf("Elementul a fost gasit la pozitia %d fata de elementul 1, pe linia %d si coloana %d",poz,i+1,j+1);
nr++;
}
}
}
if(nr>0)
{
printf("\nElementul a fost gasit de %d ori",nr);
}
}
I can't see what's wrong, any help would be appreciated , thank you.
You should declare the array after initializing n and m to non-zero values, something like this
int m, n;
if (scanf("%d%d", &n, &m) != 2)
{
/* you could write a function that tries to get input again */
printf("invalid input\n");
return -1;
}
int tablou[m][n];
also not that main() should return int.
#iharob uses the new dynamc arrays of C99. It is a nice feature with the compiler doing lots of things for you. In older chainsaw versions of C you have to do that yourself:
int m, n, *tablou;
if (scanf("%d%d", &n, &m) != 2)
{
printf("invalid input\n");
return -1;
}
if ((tablou= calloc(n*m, sizeof(int)))==NULL) return -1;
...
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
poz++;
if(tablou[ i*n + j]==e)
...
I'm doing an exercise in C, but I don't know why as first result I have always -1 (that is impossible).
I have -1 only after the swap to ordinate the array.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
main(){
int vet[100], cont[100];
int i, c, f=100;
int swap;
int r=0;
int search;
srand(time(NULL));
for(i=0;i<100;i++){
vet[i]=rand()%100+1;
}
while(r==0){
r=1;
for(i=0;i<100;i++){
if(vet[i]>vet[i+1]){
swap=vet[i+1];
vet[i+1]=vet[i];
vet[i]=swap;
r=0;
}
}
}
for(i=0;i<100;i++){
printf("%d) %d\n", i+1, vet[i]);
}
i=0;
r=0;
printf("Inserisci numero da ricercare (1-10000) -> ");
scanf("%d", &search);
if(search>10000 || search<0){
printf("Hai inserito un valore non valido\n");
}
else{
c=(i+f)/2;
while(vet[c]!=search && i<f){
if(vet[c]<search){
i=c+1;
c=(i+f)/2;
}
else if(vet[c]>search){
f=c-1;
c=(i+f)/2;
}
if(vet[c]==search){
cont[r]=c+1;
r++;
}
}
if(vet[c]!=search){
printf("Non e\' stato trovato nessun valore %d", search◆
}
else{
for(i=0;i<r;i++){
printf("%d\n", cont[i]);
}
}
}
}
Now I must use srand(time(NULL)) I know that there are better solutions.
The exercise isn't complete, now I' m trying to solve this error, someone can help me?
EDIT:
I'm using OPENVMS to compile, link and run
Your problem is probably here:
for(i=0;i<100;i++){
if(vet[i]>vet[i+1]){
When i=99, and you access vet[i+1], you are off the end of the array. This element is not defined, and it's probably just fluke that you don't get any worse behaviour.
EDIT:
So the solution is to change in
for(i=0;i<99;i++){
if(vet[i]>vet[i+1]){