Alessandro
Yes I mean, that’s probably the issue
%Nikita
You can reallocate static array by creating new one. This function reallocates static array:
void* my_realloc(void* from, void* to, size_t sfrom, size_t sto){
memset(to, 0, sto);
memcpy(to, from, sfrom);
return to;
}
and you can use it like:
int ages[] = {23, 43, 12, 89, 2}, ages2[10];
int *p = (int*)my_realloc(ages, ages2, sizeof(ages), sizeof(ages2));
printf(“copied array: “);
for (int* k = p; k < p+10; k++)
printf(“%i - “, *k);
%Nikita
Or if you don’t want to create many arrays just to reallocate some data, instead of using malloc or calloc you can create one huge array, and make your own allocate function to work with. That’s a variant too
Ster-Devs
\Device\NUL
%Nikita
Sadly stack memory is limited
I know, but I often use this trick when I don't need so many elements, an array of 255..1024 elements will be enough
the best for everyone
#include<stdio.h>
int main ()
{
char cha[100];
int i=0,j,k;
printf("Enter the words that you want to write /n");
gets(cha);
for(i=0; cha[i]!='\0'; i++) // to denote string
{
k=1; // because it will be at least one characters
for(j=i+1; cha[j]!='\0';j++) // to store repeated characters
{
cha[i]=cha[j]; // to store the new of them
}
k++;
cha [j]= ' ' ; // to remove the repetition of characters of it
}
cha[j] = ' ' ;
printf( "\n %c appcarred = %d times ",cha[i],k);
}
the best for everyone
what this app is not working
the best for everyone
Hima
the best for everyone
Hima
counting the characters
You have built yourself a very weird logic.
for(j=i+1; cha[j]!='\0';j++) // to store repeated characters
{
cha[i]=cha[j]; // to store the new of them
}
What do you want to do in this inner loop?
the best for everyone
JY
Hi anyone interested in joining a crypto project? C++, tcpip, grpc, database skills required
Hima
i wrote next to it what do i want from them
I don't get it, care to explain?
Anyways , this can be a lot more simpler.
int ch[26]={};
for(int i=0;cha[i];i++)
ch[cha[i]-'a']++;
for(int i=0;i<26;i++)
{ if(ch[i])
printf( "%c appeared = %d times ",cha[i],ch[i]);
}
the best for everyone
the best for everyone
how come that
the best for everyone
Hima
Shubh
i am using vs cod but i am input 2 values and skip the program:
Shubh
#include <stdio.h>
int main ()
{
int first,last,i,j;
printf ("Enter a first number: ");
scanf ("%d",&first);
printf ("Enter a second number: ");
scanf ("%d",&last);
for (i=first ; i<=last ; i++)
{
for (j=2;j<=i;j++)
{
if (i%j==0)
break;
}
if (i==j)
printf ("%d\n",j);
}
return 0;
}
Shubh
PS E:\language> cd "e:\language\c\c++\" ; if ($?) { gcc prime_numer_rang.c -o prime_numer_rang } ; if ($?) { .\prime_numer_rang }
Enter your first number: 10
Enter your second number: 15
PS E:\language\c\c++>
Shubh
please help me
as
\Device\NUL
Suka
Shubh
it's working thankyou
coal
Anonymous
https://i.imgur.com/B9DMnlS.png
Can someone explain these three differences between classes and structures in C++?
Null
Do anyone use gets() fxn in c or c++
Null
Oh ohkk
Null
#include<stdio.h>
#include<string.h>
main()
{
int wc,cd,diets,cost,et,price,l,total;
char a[20];
/*
printf(" *** MESS BILL ***\n");
printf("\n Enter the worker charges : Rs.");
scanf("%d",&wc);
printf("\n Enter the number of compulsory diets:");
scanf("%d",&cd);
printf("\n Enter the number of diets taken :");
scanf("%d",&diets);
printf("\n Enter the cost per diet : Rs.");
scanf("%d",&cost);
*/
printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :");
scanf("%d",&l);
printf("\n Enter the name of any extra thing taken :");
gets(a);
printf(a);
printf("\n Enter the amount :");
scanf("%d",&et);
printf ("\n Enter the price of %s : Rs.",a);
scanf("%d",&price);
if(diets>cd)
total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price);
else
total=wc+(cd*cost)+(l*20)+(et*price);
printf("\n TOTAL BILL=%d",total);
return 0;
}
Null
Please help me out here why isn't this working
Null
then how should we enter a string in c
I use Arch
\Device\NUL
\Device\NUL
There's still newline in the input stream
Null
Wdym🤔?
\Device\NUL
Wdym🤔?
scanf(" ", ...)
scanf(" ", ...)
Did you know why before you input the second scanf it receives input ?
\Device\NUL
Because scanf leave newline in the input stream, and the newline goes to next input
Null
\Device\NUL
\Device\NUL
You need to clear the input stream
Talula
#include<stdio.h>
#include<string.h>
main()
{
int wc,cd,diets,cost,et,price,l,total;
char a[20];
/*
printf(" *** MESS BILL ***\n");
printf("\n Enter the worker charges : Rs.");
scanf("%d",&wc);
printf("\n Enter the number of compulsory diets:");
scanf("%d",&cd);
printf("\n Enter the number of diets taken :");
scanf("%d",&diets);
printf("\n Enter the cost per diet : Rs.");
scanf("%d",&cost);
*/
printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :");
scanf("%d",&l);
printf("\n Enter the name of any extra thing taken :");
gets(a);
printf(a);
printf("\n Enter the amount :");
scanf("%d",&et);
printf ("\n Enter the price of %s : Rs.",a);
scanf("%d",&price);
if(diets>cd)
total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price);
else
total=wc+(cd*cost)+(l*20)+(et*price);
printf("\n TOTAL BILL=%d",total);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
int wc,cd,diets,cost,et,price,l,total;
char a[20];
/*
printf(" *** MESS BILL ***\n");
printf("\n Enter the worker charges : Rs.");
scanf("%d",&wc);
printf("\n Enter the number of compulsory diets:");
scanf("%d",&cd);
printf("\n Enter the number of diets taken :");
scanf("%d",&diets);
printf("\n Enter the cost per diet : Rs.");
scanf("%d",&cost);
*/
printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :");
scanf("%d",&l);
printf("\n Enter the name of any extra thing taken :");
scanf("%s",a);
printf("%s\r\n",a);
printf("\n Enter the amount :");
scanf("%d",&et);
printf ("\n Enter the price of %s : Rs.",a);
scanf("%d",&price);
if(diets>cd)
total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price);
else
total=wc+(cd*cost)+(l*20)+(et*price);
printf("\n TOTAL BILL=%d",total);
return 0;
}
Null
Null
#include<stdio.h>
#include<string.h>
int main()
{
int wc,cd,diets,cost,et,price,l,total;
char a[20];
/*
printf(" *** MESS BILL ***\n");
printf("\n Enter the worker charges : Rs.");
scanf("%d",&wc);
printf("\n Enter the number of compulsory diets:");
scanf("%d",&cd);
printf("\n Enter the number of diets taken :");
scanf("%d",&diets);
printf("\n Enter the cost per diet : Rs.");
scanf("%d",&cost);
*/
printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :");
scanf("%d",&l);
printf("\n Enter the name of any extra thing taken :");
scanf("%s",a);
printf("%s\r\n",a);
printf("\n Enter the amount :");
scanf("%d",&et);
printf ("\n Enter the price of %s : Rs.",a);
scanf("%d",&price);
if(diets>cd)
total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price);
else
total=wc+(cd*cost)+(l*20)+(et*price);
printf("\n TOTAL BILL=%d",total);
return 0;
}
But it won't take blank spaces
Null
Ohkk 🤔
Null
But there is no scan in my code before that one
Talula
Null
Yea but that is when we use scaning statement before that 🤔 if i am right??
\Device\NUL
*Confused nick young
Null
Yea i am confused 😅
Talula
Yea i am confused 😅
#include<stdio.h>
#include<string.h>
int main()
{
int wc,cd,diets,cost,et,price,l,total;
char a[20];
/*
printf(" *** MESS BILL ***\n");
printf("\n Enter the worker charges : Rs.");
scanf("%d",&wc);
printf("\n Enter the number of compulsory diets:");
scanf("%d",&cd);
printf("\n Enter the number of diets taken :");
scanf("%d",&diets);
printf("\n Enter the cost per diet : Rs.");
scanf("%d",&cost);
*/
printf("\n Enter the numbers of packets of Lays(cost 20 rs.) :");
scanf("%d",&l);
getc(stdin);
printf("\n Enter the name of any extra thing taken :");
fgets(a,sizeof(a),stdin);
printf("%s\r\n",a);
printf("\n Enter the amount :");
scanf("%d",&et);
printf ("\n Enter the price of %s : Rs.",a);
scanf("%d",&price);
if(diets>cd)
total=wc+(cd*cost)+((diets-cd)*cost)+(l*20)+(et*price);
else
total=wc+(cd*cost)+(l*20)+(et*price);
printf("\n TOTAL BILL=%d",total);
return 0;
}
Talula
This should work...
Null
Why you put getc(Stdin)?
Talula
Because when you press enter '\n' remains in the buffer, when you put getc(stdin) it takes that char and does nothing.
Talula
Basically clearing the buffer for stdin
Anonymous
Thanks For Warm Welcoming 🌹Rose!
Null
Thanks alot @Tazmikar and @kawaiighost
\Device\NUL
\Device\NUL
Another way to store string without getting newline from stdin (get string using fgets) is using scanf with max field width
Ex:
#define MAX 100
/* one more bytes for NULL char */
char str[MAX + 1];
scanf("%100[^\n]", str);
getchar();
the best for everyone
// Two dimensional array with a function and how to sum them togother and get the average of them and the maximum average and sum only the students
#include<stdio.h>
void TwoDimensionalArray(int subjects [2][5])
{
int i=0,j=10,sumStudents=0,allSum=0,average=0,maxSubject=0;
for(i=0;i<2;i++)
{
printf("student number is %d ",i);
for(j=0;j<5,j++)
{
printf("student number is %d and subjects number is %d ",subjects[i][j] );
allSum+=subjects[i][j];
sumStudents+= subjects[i][j] ;
average= sumStudents /subjects[i][j];
maxSubject = ;
}
printf("the sum of the students is %d ",sumStudents);
sumStudents=0;// because he wants to sum students one by one
}
printf("the whole sum of subjects is %d",allSum);
printf("\n");
}
int main()
{
printf("Hello wold");
int marks[][5]={ {10,20,30,40,50} , {40,50,80,10,30} };
marks[0][0] = 10;
marks[0][1] = 10;
marks[0][2] = 10;
marks[0][3] = 10;
marks[0][4] = 10;
for(int i=0;i<2;i++)
{
for(int j=0;j<5;j++)
{
printf("Please Enter the mark of the student number");
scanf("%d "marks[i][j]);
}
}
int TwoDimensionalArray(marks,[2][5]); // passing by reference
}
the best for everyone
i have a problem in this code
the best for everyone
can anyone help me to know about the problem of it