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.
Related
the code doesn't work and i don't know why but i'm a begginer in this language so the fault could be mine
#include <stdio.h>
int horascomeco(){
int horasco; int minutosco; int totalcomeco;
printf("-----A que horas e minutos começou o recolher?\nHoras:");
scanf("%d", &horasco);
printf("Minutos:");
scanf("%d", &minutosco);
printf("R:O recolher começou ás %d horas e %d minuto\n\n\n", horasco, minutosco);
totalcomeco = horasco*60 + minutosco;
printf("%d\n", totalcomeco);
return totalcomeco;
}
int horasapanhado(){
int horasapa; int minutosapa;int totalapanhado;
printf("-----A que horas e minutos foi apanhado?\nHoras:");
scanf("%d", &horasapa);
printf("Minutos:");
scanf("%d", &minutosapa);
printf("R:Voce foi apanhado ás %d horas e %d minutos\n\n\n", horasapa, minutosapa);
totalapanhado = horasapa*60 + minutosapa;
printf("%d\n", totalapanhado);
return totalapanhado;
}
int tempodemulta(){
int minutosforadalei;
int horascomeco;
printf("%d\n", horascomeco);
int horasapanhado;
printf("%d\n", horasapanhado);
minutosforadalei = horasapanhado - horascomeco;
printf("A sua multa vai ser respetiva a %d minutos fora do recolher obrigatorio", minutosforadalei);
}
int main(){
horascomeco();
horasapanhado();
tempodemulta();
return 0;
}
why appears 14705... ? minutosforadalei should be = 1 in that print
int tempodemulta(){
int minutosforadalei;
int horascomeco;
printf("%d\n", horascomeco); // no value assigned to horascomeco!
int horasapanhado;
printf("%d\n", horasapanhado); // no value assigned to horaspanhado!
minutosforadalei = horasapanhado - horascomeco;
printf("A sua multa vai ser respetiva a %d minutos fora do recolherobrigatorio", minutosforadalei);
}
You are printing and calculating with variables that you have not assigned any value to yet. You have to assign a value to a variable before you try to use that value.
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;
}
im trying to make this function work but the array keeps getting filled with trash. The cicle is running properly, I just cant seem to find the error.
int CargaArregloEnteros(int a[], int dim, int v);
void MostrarArreglo(int n[], int v);
int main()
{ int notas[100], validos=0;
validos = CargaArregloEnteros(notas, 100, validos);
printf("\nLos validos son %d\n", validos);
MostrarArreglo(notas, validos);
return 0;
}
int CargaArregloEnteros(int a[], int dim, int v)
{
char opcion=0;
while(v<dim && opcion !=27)
{
printf("\n Ingrese una nota a cargar ");
fflush(stdin);
scanf("&d", &a[v]);
printf("\n Presione ESCAPE para salir\n");
fflush(stdin);
opcion=getch();
system("cls");
v++;
}
return v;
}
void MostrarArreglo(int n[], int v)
{
for(int i=0;i<v;i++)
{
printf("%d | ", n[i]);
}
}
You need to change
scanf("&d", &a[v]);
to
scanf("%d", &a[v]);
so that it constitutes a valid conversion specifier syntax.
Also, a good lesson on why it's best to check the return value of scanf() and other library functions.
I am trying to write a program that calculates the length of one side of a triangle with the help of the Pythagoras equation (c²=a²+b²). The user must have the option to choose what side he want to calculate, this is what I have tried:
#include <conio.h>
#include <stdio.h>
#include <math.h>
// Pitagora:
// c=sqrt(pow(a,2)+pow(b,2));
// a=sqrt(pow(c,2)-pow(b,2));
// b=sqrt(pow(c,2)-pow(a,2));
int cateta(int x, int y){
int cat;
printf("Dati marimea lui:");
scanf("%d", &x);
printf("Dati marimea lui:");
scanf("%d", &y);
cat=sqrt(pow(x,2)-pow(y,2));
return cat;
}
int main(){
int a,b,c;
char l;
printf("Ce latura doriti sa aflati?");
printf("\n c : ipotenuza\n a : cateta alaturata\n b : cateta opusa\n");
printf("Introduceti litera laturei respective : ");
scanf("%s", &l);
if (l == a){
a=cateta(c,b);
printf("Marimea catetei alaturate este: %d", a);
}
else if (l == b){
b=cateta(c,a);
printf("Marimea catetei opuse este: %d", b);
}
else {
c=sqrt(pow(a,2)+pow(b,2));
printf("Marimea ipotenuzei este: %d", c);
printf("\n");
}
getch ();
return 0;
}
But, for some reason when I give a value of a to the variable &l the program displays the content of this piece of code: printf("Marimea ipotenuzei este: %d", c); instead of scaning the value of x and y, and terminates. Here is a picture with the result: https://www.dropbox.com/s/wzk3osw1t8729et/Untitled.png?dl=0
you are using %s in scanf() for a character type variable, instead use this
scanf(" %c", &l);
First, u need to change the scanf statement to scanf( "%c",&l);
Now the variable l contains the character entered by user.
Next, during comparison change the if condition to if(l=='a') , if( l=='b') as a and b are character literals.
With this changes, the program should work!
Happy coding!!
I'm having trouble with this little piece of code. The problem is related to the data entry. I can't get the data as they are typed. If I enter the following values:
g1 = 3; q = 3 and term(termo) = 3
I get
g1 = 3, q = 0 and term(termo) = 2686724.
Already tried using getchar() after scanf, and fflush(stadin) before and after data input routine, and also put space between "% f (d)". Nothing works.
I am using DevC ++ and the CodeBlocks and both have the same problem (could be the gnu gcc?).
I don't know what else to do. Is the code, is the scanf() or is my PC that's in trouble?
This try to be a C code. The problem is in termos_pg() function routine. The sentence printf("g1=%.f, q=%.f, termo=%d,\n",g1,q,termo); into calcula_pg() function routine shows what I get into termos_pg() routine. Can anybody help me?
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <math.h>
float ind,gn,g1,val,q;
int termo,gx=0,i;
main()
{
setlocale(LC_ALL,"Portuguese");
char X;
printf ("\nSe houver parâmetro desconhecido, tecle s ou S, caso nao, ENTER.\n");
X=getch();
fflush(stdin);
if(X=='s' || X=='S'){
es_pg();
return (0);}
else{
termos_pg();
return (0);
}
system("PAUSE");
return 0;
}
termos_pg()
{
printf("\nDigite o 1º termo da PG: ");
scanf("%f",&g1);
printf("Digite a razao da PG: ");
scanf("%f",&q);
printf("Digite a quantidade de termos: ");
scanf("%d",&termo);
calcula_pg(g1,q,termo);
}
calcula_pg(g1,q,termo)
{
printf("g1=%.f, q=%.f, termo=%d,\n",g1,q,termo);
printf("\n\tA sequência é: \n");
for(i=0;i<termo;i++)
if(++gx==termo)
printf("\ta%d= %20.f.\n",gx,(g1*powf(q,(termo-(termo-i)))));
else
printf("\ta%d= %20.f;\n",gx,(g1*powf(q,(termo-(termo-i)))));
system("PAUSE");
}
es_pg(){;}
You haven't given any types for your arguments to calcula_pg, which means that the compiler assumes you're passing int-typed variables, not float.
Here's an example program to demonstrate the effect:
#include <stdio.h>
void ints(int x, int y, int z)
{
printf("ints: x == %.f, y == %.f, z == %d\n", x, y, z);
}
void floats(float x, float y, int z)
{
printf("floats: x == %.f, y == %.f, z == %d\n", x, y, z);
}
int main()
{
ints(3.14, 3.14, 5);
floats(3.14, 3.14, 5);
return 0;
}
Here's the output:
ints: x == 0, y == 0, z == 3
floats: x == 3, y == 3, z == 5
Add fflush(stdout) after every printf call before scanf