How can I count the average separately for all rows - c

It's a basic program, but I need to find out how to calculate the average for each row separately. Should I use more counts or should I use another loop?
int main()
{
int r;
int Count=0;
double sum=0;
double Average=0;
double c,value;
for (r=1; r<11; r++)
{
for(c=1; c<5; c++)
{
value=(rand()%10000)/100.0;
if (value>=67.0)
{
Count++;
printf("%5.2f ",value);
sumTotal=sumRow/sumCol;
sum += value;
}
else
{
printf(" ");
}
}
Average=sum/Count;
if(Average == 0){
printf("| ");
}
else{
printf("| %6.2f ",Average);
}
printf("\n");
}
return 0;
}

make the counters in outer loop
int main()
{
int r;
double c,value;
for (r=1; r<11; r++)
{
int Count=0;
double sum=0;
double Average=0;
for(c=1; c<5; c++)
{
value=(rand()%10000)/100.0;
if (value>=67.0)
{
Count++;
printf("%5.2f ",value);
// sumTotal=sumRow/sumCol;
sum += value;
}
else
{
printf(" ");
}
}
Average=sum/Count;
if(Average == 0){
printf("| ");
}
else{
printf("| %6.2f ",Average);
}
printf("\n");
}
return 0;
}
I commented out broken code.

Related

Getting negative numbers or zero as output of factorial in C

I've written a very simple program which calculate the factorial of a number, the problem is that from 30 to 60 approximately, it returns a negative number and from 70 it returns 0.
I don't what I've done wrong. Could this problem depend on the computing power of my computer?
Here's the code:
#include <stdio.h>
int main(){
int x, i;
long long int f = 1;
printf("Insert a number:");
scanf("%d", &x);
if (x == 0){
printf("0! = 1");
}
else {
for (i = 1; i <= x; i++){
f *= i;
}
printf("%d! è = %lli", x, f);
}
return 0;
}
Here is my code:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void Carry(int bit[],int pos)
{
int i,carray=0;
for(i=0;i<=pos;i++)
{
bit[i]+=carray;
if(bit[i]<=9)
{
carray=0;
}
else if(bit[i]>9&&i<pos)
{
carray=bit[i]/10;
bit[i]%=10;
}
else if(bit[i]>9&&i>=pos)
{
while(bit[i]>9)
{
carray=bit[i]/10;
bit[i]%=10;
i++;
bit[i]=carray;
}
}
}
}
int main()
{
int num,pos,digit,i,j,m,n;
double sum=0;
int *fact;
printf("input want to calculute factorial num:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum+=log10(i);
}
digit=(int)sum+1;
if(!(fact=(int *)malloc((digit+1)*sizeof(int))))
{
printf("malloc failed\n");
return 0;
}
for(i=0;i<=digit;i++)
{
fact[i]=0;
}
fact[0]=1;
for(i=2;i<=num;i++)
{
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
for(j=0;j<=pos;j++)
{
fact[j]*=i;
}
Carry(fact,pos);
}
for(j=digit;j>=0;j--)
{
if(fact[j]!=0)
{
pos=j;
break;
}
}
m=0;
n=0;
for(i=pos;i>=0;i--)
{
printf("%d",fact[i]);
m++;
if(m%4==0)
{
printf(" ");
}
if(m==40)
{
printf("\n");
m=0;
n++;
if(n==10)
{
printf("\n");
n=0;
}
}
}
printf("\n\n");
return 0;
}

Star Pattern in C- 2nd half is not printing

I've been trying to print star pattern but it's not working after
"if(i>4)" as you can see the second image there's a gap in between which implies that the spaces are getting printing or the new line but the star pattern is not getting printed
enter code here
//the first half
int main() {
int i,j,k,m;
for (i=1;i<=4;i++) {
for (j=1;j<=4-i;j++) {
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n"); // to print the first half
}
//the second half
if(i>4){
for(j=1;j<=i-2;j++)
{
for (k=1;k<=j;k++) {
printf(" ");
}
for(m=1;m<=4-i;m++)
{
printf("*");
}
printf("\n");
}
}
return 0;
}
Try this
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
n = n/2+1;
for(int i=1;i<=n;i++)
{
for(int j=n-i;j>=1;j--)
{
printf(" ");
}
for(int k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
for(int i=1;i<=n;i++)
{
for(int k=1;k<=i;k++)
{
printf(" ");
}
for(int j=n-i;j>=1;j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output

Union, intersection and difference of three arrays

In this code I try to create three arrays. With these three arrays I successflly tried to make the union. But when I want to make an Intersection and the difference I couldn't.
#include <stdio.h>
int main()
{
int dizi1Sinir, dizi1Deger; // ilk alt kumenin degiskenleri
int dizi2Sinir, dizi2Deger; // ikinci alt kumenin degiskenleri
int dizi3Sinir, dizi3Deger; // ucuncu alt kumenin degiskenleri
// BIRINCI ALT KUME TANIMLAMA ISLEMLERI
printf("ilk alt kumen kac elemanli olsun?\n");
scanf("%d", &dizi1Sinir);
int alt_kume_1[dizi1Sinir]; // ilk alt kume
for(dizi1Deger = 0; dizi1Deger < dizi1Sinir; dizi1Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi1Deger + 1);
scanf("%d", &alt_kume_1[dizi1Deger]);
}
// -------------------------------------------------------------------------------
//IKINCI ALT KUME TANIMLAMA ISLEMLERI
printf("ikinci alt kumen kac elemanlı olsun?\n");
scanf("%d", &dizi2Sinir);
int alt_kume_2[dizi2Sinir]; // ikinci alt kume
for(dizi2Deger = 0; dizi2Deger < dizi2Sinir; dizi2Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi2Deger + 1);
scanf("%d", &alt_kume_2[dizi2Deger]);
}
//UCUNCU ALT KUME TANIMLAMA ISLEMLERI
printf("ucuncu alt kumen kac elemanli olsun?\n");
scanf("%d", &dizi3Sinir);
int alt_kume_3[dizi3Sinir]; // ucuncu alt kume
for(dizi3Deger = 0; dizi3Deger < dizi3Sinir; dizi3Deger++)
{
printf("dizinin elemanlarini gir.\n %dnci eleman = ", dizi3Deger + 1);
scanf("%d", &alt_kume_3[dizi3Deger]);
}
//---------------------------------
int azami=dizi1Sinir+dizi2Sinir+dizi3Sinir;
int birlesim[azami];
int i;
for(i=0;i<azami;i++)
{
if(i<dizi1Sinir)
birlesim[i]=alt_kume_1[i];
else if(i-dizi1Sinir<dizi2Sinir)
birlesim[i]=alt_kume_2[i-dizi1Sinir];
else
birlesim[i]=alt_kume_3[i-dizi1Sinir-dizi2Sinir];
}
//-----------------------------
//Ayni Elemanlari Sil (-1)
for(i=0;i<azami;i++)
{
int tempSayi=birlesim[i];
int j;
for(j = 0;j<i;j++)
{
if(birlesim[j]==tempSayi)
{
birlesim[i]=-1;
}
}
}
printf("\nbirlesim = ");
for(i = 0; i < azami; i++)
{
if(birlesim[i]!=-1)
printf("%d ", birlesim[i]);
}
int main() {
//************************
printf("\ninsertion dizi_1 and dizi_2 : ");
intersection_two_arrays(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir);
printf("\ninsertion dizi_1 and dizi_3 : ");
intersection_two_arrays(alt_kume_1,dizi1Sinir,alt_kume_3,dizi3Sinir);
printf("\ninsertion dizi_2 and dizi_3 : ");
intersection_two_arrays(alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
printf("\ninsertion dizi_1 dizi_2 and dizi_3 : ");
intersection_three_arrays(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
//...etc
//*************************
printf("\nDifference dizi_1 - dizi_2 : ");
differenceX_Y(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir);
printf("\nDifference dizi_2 - dizi_1 : ");
differenceX_Y(alt_kume_2,dizi2Sinir,alt_kume_1,dizi1Sinir);
printf("\nDifference dizi_2 - dizi_3 : ");
differenceX_Y(alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
printf("\nDifference dizi_1 - dizi_2 and dizi_3 : ");
differenceX_YandZ(alt_kume_1,dizi1Sinir,alt_kume_2,dizi2Sinir,alt_kume_3,dizi3Sinir);
//...etc
//*************************
}
function 1 : Intersection of two sets
void intersection_two_arrays(int kume_1[], int sinir1 , int kume_2[] , int sinir2)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir2;
else value=sinir1;
int hold[value];
int h=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
hold[h++] = kume_1[i];
continue;
}
}
}
if(h==0) {
printf("There is not common value");
}
else {
for(i=0; i<h; i++) {
printf("%d ", hold[i]);
}
}
}
function 2 : Intersection of three sets
void intersection_three_arrays(int kume_1[], int sinir1 , int kume_2[] , int sinir2,
int kume_3[], int sinir3)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir2;
else value=sinir1;
int hold[value];
int h=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
hold[h++] = kume_1[i];
continue;
}
}
}
intersection_two_arrays(hold,h, kume_3,sinir3);
}
function 3 : Difference X-Y
void differenceX_Y(int kume_1[], int sinir1 , int kume_2[] , int sinir2)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir1;
else value=sinir2;
int hold[value];
int h=0;
int findEqual=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
findEqual++;
continue;
}
}
if(findEqual==0) hold[h++] = kume_1[i];
findEqual=0;
}
if(h==0) {
printf("There is not different value.");
}
else {
for(i=0; i<h; i++) {
printf("%d ", hold[i]);
}
}
}
function 4 : Difference X-(Y and Z)
void differenceX_YandZ(int kume_1[], int sinir1 , int kume_2[] , int sinir2, int
kume_3[], int sinir3)
{
int i,j;
int value;
if(sinir1>sinir2) value=sinir1;
else value=sinir2;
int hold[value];
int h=0;
int findEqual=0;
for(i=0; i<sinir1; i++) {
for(j=0; j<sinir2; j++) {
if(kume_1[i] == kume_2[j]) {
findEqual++;
continue;
}
}
if(findEqual==0) hold[h++] = kume_1[i];
findEqual=0;
}
differenceX_Y(hold, h , kume_3, sinir3);
}

Function string_to_table is breaking on 3rd if c

I am trying to create program to add two matrixes. After typing input like [12 4] program crashes when function strcat starts.
I have no idea what is wrong. func.h consists of stdio.h, _mingw.h,stdlib.h and string.h
#include"func.h"
void string_to_table(double **matrix, char inpt[])/*problematic function*/
{
printf("convertion start");
int i=strlen(inpt);
printf("%i",i);
int j=1;
int k=0,l=0;
char num[128];
double converted=0;
printf("breakpoint1");
while(j<(i-1))
{
if(inpt[j]==' ')
{
printf("first if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++l;
printf("breakpoint2");
}
else if(inpt[j]==';')
{
printf("second if");
converted=atof(num);
num[0]='\0';
matrix[k][l]=converted;
++k;
l=0;
}
else
{
printf("third if");
strcat(num,inpt[j]);/*place when everything crashes*/
}
++j;
}
printf("convert0 end");
}
void add_matrix(double **matrix1, double **matrix2,int i,int j)
{
int k=0;
int l=0;
while(k<i)
{
while(l<j)
{
matrix1[k][l]+=matrix2[k][l];
++l;
}
l=0;
++k;
}
int matrixproccesing(int *i,int *j, char m[])/*sprawdzanie poprawnosci wejscia*/
{
printf("macro start");
int columnnum=0,rownum=0,x=0,piv=0,check=0;
int textsize=strlen(m);
printf("%i",i);
printf("loop start");
printf("%i",textsize);
while(x<(textsize-1))
{
printf("%i",x);
printf("\n");
if(x==0)/*czy poczatek to [*/
{
if(m[x]!='[')
return 0;
}
else if(x==(textsize-2))/*czy koniec to]*/
{
printf("kohec");
if(m[x]==']')
break;
return 0;
}
else if((m[x]>47&&m[x]<58)||(m[x]==' ')||(m[x]=='.')||(m[x]==';')||(m[x]=='-'))/*czy liczba*/
{
if(m[x]==';')/*czy ilosc liczb w rzedzie taka sama*/
{
if(check==0)
{
check=columnnum;
}
else if(check!=columnnum)
{
return 0;
}
printf("colnum");
columnnum=0;
rownum++;
}
else if(m[x]==' ')/*czy nowa liczba/kolumna */
{
columnnum++;
}
}
++x;
}
*i=(check+1);
*j=(columnnum+1);
printf("macro end");
return 1;
}
int is_same_size(int a, int b,int c ,int d)/*test rozmiaru*/
{
if((a==c)&(b==d))
return 1;
return 0;
}
void print_da_matrix(double **matrix, int i, int j)
{
int k=0,l=0;
printf("[ ");
while(k<i)
{
while(l<j)
{
printf("%f",matrix[k][l]);
printf(" ");
}
printf(";");
l=0;
if(k<(i-1))
++k;
}
printf("]");
}
void release_the_memory(double **matrix, int i)
{
int k=0;
while(k<i)
{
free(matrix[k]);
++k;
}
free(matrix);
matrix=NULL;
}
}
int main()
{
int i=0,j=0,m1=0,m2=0,tabcr=0;
char matrix[512];
fgets(&matrix,511,stdin);
double **matrix1;
double **matrix2;
if(!matrixproccesing(&i,&j,matrix))
{
printf("zle wejscie");
return 0;
}
matrix1=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix1[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix1,matrix);
printf("\n");
printf("podaj druga macierz");
fgets(&matrix,511,stdin);
if(!matrixproccesing(&m1,&m2,matrix))
{
printf("zle wejscie");
return 0;
}
tabcr=0;
if(!is_same_size(i,j,m1,m2))
{
printf("matrixes have different size.");
return 0;
}
matrix2=(double**)malloc(i*sizeof(double *));
while(tabcr<j)
{
matrix2[tabcr]=(double*)malloc(j*sizeof(double));
++tabcr;
}
string_to_table(matrix2,matrix);
add_matrix(matrix1,matrix2,i,j);
/* print_da_matrix(matrix1,i,j);
release_the_memory(matrix1,i);
release_the_memory(matrix2,i);*/
return 0;
}

Nptel-pascal programming assignment

i am pursuing the course on programming and data structures in nptel's MOOC.
A programming assignment in the course requires us to calculate sum of selected coefficients.
Now the program i wrote calculated the answer , but i am experiencing a runtime error.
Now i had this inclination of using gets() instead of scanf() so as to speed up my input.
how do i do that?
the code is as follows:
#include<stdio.h>
int combi(int ,int);
long fact(int);
int main()
{
int r,i,v[20],p[20],t,l=1,b=0,sum=0,a=0,flag=0;
char ch='a';
scanf("%d",&r);
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&v[0]);
if(getchar()==' ')
{
if(r<v[0])
{
while(ch!='\n')
{
scanf("%d",&v[l]);
l++;
if(getchar()=='\n')
{
p[a]=-1;
a++;
l=1;
break;
}
}
}
else
{
while(ch!='\n')
{
scanf("%d",&v[l]);
if(v[l]>v[0])
{
flag=1;
}
l++;
if(getchar()=='\n')
{
if(flag>0)
{
p[a]=-1;
a++;
l=1;
sum=0;
flag=0;
break;
}
else
{
for(b=1;b<l;b++)
{
sum=sum+combi(v[0],v[b]);
}
p[a]=sum;
a++;
sum=0;
l=1;
break;
}
}
}
}
}
}
for(i=0;i<t;i++)
{
printf("%d\t ",p[i]);
}
printf("\n");
}
int combi(int x,int y)
{
int a=fact(x)/(fact(x-y)*fact(y));
return a;
}
long fact(int z)
{
int i=1;
long f=1;
while(i<=z)
{
f=f*i;
i++;
}
return f;
}

Resources