My for loops seems not working properly. First number which tells me how many task do I want my program to do is n. When I input 1 or 2, it works, but when I input 3 and more, it starts to struggle. Every row has 3 numbers separated by a space as you can see in the code. I am not getting all outputs. Algorithm in this program works perfect so no problem there.
Please ignore comments in the code. And sorry for my english.
#include <stdio.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int r[];
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
//printf ( "max 1000 uloh min 1 \n");
return 1;//scanf("%d", &n);
}
for( i=1; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000)) {
//printf ("prekroceny limit \n");
return 1; // scanf("%d %d %d", &s, &p, &d);
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] == 0;
}
else
{
//s = k * d + A ;
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < i+1 ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
system("pause");
}
Example input so you can understand better:
4
5 4 4
6 100 3
500 5 1000
314 159 26
and output:
3
0
3
147
EDIT>
5
3434 234 2345
14455 345 12
134 145 1345
9242 2455 13455
83 34 133
output:
126
144
141
769
13
or something shorter>
input:
2
15000 1 1
1 4000 1
output:
0
0
Im getting return value 1 in both examples
My final code edit:
#include <stdio.h>
#include <stdlib.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int *r;
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
return 0;//printf ( "max 1000 uloh min 1 \n");
//scanf("%d", &n);
}
r = (int *)malloc(n*sizeof(int));
for(i=0; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000))
{
//printf ("prekroceny limit \n");
return 0; //scanf("%d %d %d", &s, &p, &d);
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] = 0;
}
else
{
//s = k * d + A ;
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < n ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
free(r);
return 0;
}
You need to use malloc() to allocate memory dynamically. (Also #include stdlib.h)
For instance, add the following line after the first while loop :
//Note - at the top, change int r[] to the below:
int *r; //Instead of int r[]
//End note
while( !(n >= 1 && n <=1000) )
{
printf ( "max 1000 uloh min 1 \n");
scanf("%d", &n);
}
r = (int *) malloc(n*sizeof(int)); //Allocate "n" integers to your 'array r'
It should also be noted that once you are done using the array r, you must free() this memory like so:
free(r);
In addition, there are some other coding problems:
Be sure for( i=1; i < n; i++) is what you want. You use "i" to index your array, but arrays in C start from 0. So it (likely) should read for( i=0; i < n; i++).
r[i] == 0; is used falsely as an assignment. Use a single = to assign a value to a variable. Use the double equals == to compare two values.
for ( koniec = 0 ; koniec < i+1 ; koniec++ ) will likely through an error. Your i will be one less than n from the for-loop above it. So trying to iterate to i+1 or n will result in an array out of bounds problem -- aka a Segmentation Fault.
Edit - This should do what you want:
#include <stdio.h>
#include <stdlib.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int *r;
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
//printf ( "max 1000 uloh min 1 \n");
//scanf("%d", &n);
return 1;
}
r = (int *)malloc(n*sizeof(int));
for(i=0; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000))
{
//printf ("prekroceny limit \n");
//scanf("%d %d %d", &s, &p, &d);
return 1;
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] = 0;
}
else
{
s = k * d + A ; //This line does nothing... Just so you know
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < n ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
free(r);
system("pause");
return 0;
}
Input:
4
5 4 4
6 100 3
500 5 1000
314 159 26
Output:
3
0
3
147
Allocate space for r[].
Change assignment.
Change for loop range.
//int r[];
main() {
scanf("%d", &n);
// while( !(n >= 1 && n <=1000) )
if (!(n >= 1 && n <=1000) ) {
return 1;//scanf("%d", &n);
}
int r[n]; // allocate
// for( i=1; i < n; i++)
for( i=0; i < n; i++) // Change range (#MrHappyAsthma)
...
// r[i] == 0;
r[i] = 0; // change
r[i] = p - x ;
// for ( koniec = 0 ; koniec < i+1 ; koniec++ )
for ( koniec = 0 ; koniec < n ; koniec++ )
Related
My code is not running the loop. I've tried changing it from for to while loop, tried changing the condition for prime number but still the same response. I've attached my output in this program output image.
#include<stdio.h>
int main(void) {
int a, b, flag = 0;
int i, j;
printf("Enter lower limit of range\n");
scanf("%d", &a);
printf("Enter upper limit of range\n");
scanf("%d", &b);
printf("The prime numbers between %d and %d are\n", a, b);
i = a;
j = 2;
while (i <= b) {
while ( j < i/2 )
{
if (i % j == 0)
{
flag = 1;
}
j++;
}
if (flag == 0)
{
printf("%d\n", i);
}
i++;
}
return 0;
}
You need at least to reset the variable flag to 0 and j to 2 in each iteration of the outer loop. Also the condition j < i /2 should be changed to j <= i / 2 (for example when i is equal to 4).
while (i <= b) {
flag = 0;
j = 2;
while ( flag == 0 && j <= i/2 )
{
if (i % j == 0)
{
flag = 1;
}
j++;
}
if (flag == 0)
{
printf("%d\n", i);
}
i++;
}
Pay attention to that the program can incorrectly consider 2 as a non-prime number and 1 as a prime number.
I would write the loops the following way
for ( ; i <= b; i++ )
{
int prime = i % 2 == 0 ? i == 2 : ( i == 3 ) || ( i != 1 && i % 3 != 0 );
for ( int j = 5; prime && j <= i / j; j += 6 )
{
prime = i % j != 0 && i % ( j + 2 ) != 0;
}
if ( prime )
{
printf("%d\n", i);
}
}
Or a more simple variant
for ( ; i <= b; i++ )
{
int prime = i % 2 == 0 ? i == 2 : i != 1;
for ( int j = 3; prime && j <= i / j; j += 2 )
{
prime = i % j != 0;
}
if ( prime )
{
printf("%d\n", i);
}
}
You should declare the variables a and b as having the type unsigned int instead of int to avoid using negative numbers.
Here is a demonstration program.
#include <stdio.h>
int main( void )
{
unsigned int a = 0, b = 0;
printf( "Enter lower limit of range " );
scanf( "%u", &a );
printf( "Enter upper limit of range " );
scanf( "%u", &b );
printf( "The prime numbers between %u and %u are\n", a, b );
for ( unsigned int i = a; i <= b; i++ )
{
int prime = i % 2 == 0 ? i == 2 : i != 1;
for ( unsigned int j = 3; prime && j <= i / j; j += 2 )
{
prime = i % j != 0;
}
if (prime)
{
printf( "%u ", i );
}
}
putchar( '\n' );
}
The program output might look like
Enter lower limit of range 0
Enter upper limit of range 100
The prime numbers between 0 and 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
I need to do the following pattern using do-while, while or for.
I tried the following code but it prints the pattern only 1-5
I also tried to alter the n being 10 but then the spacing goes nuts.
#include <stdio.h>
int main(void)
{
int n = 5, i, j, num = 1, gap;
gap = n - 1;
for ( j = 1 ; j <= n ; j++ )
{
num = j;
for ( i = 1 ; i <= gap ; i++ )
printf(" ");
gap --;
for ( i = 1 ; i <= j ; i++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( i = 1 ; i < j ; i++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
Try replacing both occurences of:
printf("%d", num);
with
printf("%d", num % 10);
Now only the last digit will be shown.
After the change, for n set to 10 the program produces:
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210
I need to find elements of array which are hold in two of three given arrays.
It seems easy, but it's quite dificult and i have been strugling with this for few days.
I hope you can help me..
For input:
1 2 3 5
1 2 4 6 7
1 3 4 8 9 10
Output should be 3 (because 3,4,2 are common for two arrays)
for input
1 2 3 4
2 3 4
3 4 1
Output should be: 2 (because 1 is common for two arrays)
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main()
{
int duzina_prvog_niza = 0, duzina_drugog_niza = 0, duzina_treceg_niza = 0; //deklaracija duzina nizova
printf("Unesite broj clanova prvog niza:\n"); // unosimo duzine nizova i elemente nizova
do
{
scanf("%d", &duzina_prvog_niza);
} while (0 > duzina_prvog_niza || duzina_prvog_niza > 50); // mozda ne bi trebalo stavljati gornju granicu za duzinu niza
int niz1[duzina_prvog_niza]; //zavisi kako vam sistem provjere radi, mislim da nece praviti problem
// alociramo niz odgovarajuce duzine, iterativno popunimo niz uz odgovarajucu provjeru
for (int i = 0; duzina_prvog_niza > i; i++)
{
do
{
scanf("%d", &niz1[i]);
} while (0 > niz1[i] || niz1[i] > 10); // citaj elemente sve dok ne ucitas cifre iz odgovarajuceg opsega
}
for (int i = 0; duzina_prvog_niza > i; i++)
printf(" %d ", niz1[i]);
printf("\n");
// ** drugi niz ** -- bilo bi zgodno ovo sve strpati u jednu fju, meni je ovako bilo lakse.. c/p
printf("Unesite broj clanova drugog niza:\n");
do
{
scanf("%d", &duzina_drugog_niza);
} while (0 > duzina_drugog_niza || duzina_drugog_niza > 50);
int niz2[duzina_drugog_niza];
// alociramo niz odgovarajuce duzine, iterativno popunimo niz uz odgovarajucu provjeru
for (int i = 0; duzina_drugog_niza > i; i++)
{
do
{
scanf("%d", &niz2[i]);
} while (0 > niz2[i] || niz2[i] > 10); // citaj elemente sve dok ne ucitas cifre iz odgovarajuceg opsega
}
for (int i = 0; duzina_drugog_niza > i; i++)
printf(" %d ", niz2[i]);
printf("\n");
// ** treci niz **
printf("Unesite broj clanova treceg niza:\n");
do
{
scanf("%d", &duzina_treceg_niza);
} while (0 > duzina_treceg_niza || duzina_treceg_niza > 50);
int niz3[duzina_treceg_niza];
// alociramo niz odgovarajuce duzine, iterativno popunimo niz uz odgovarajucu provjeru
for (int i = 0; duzina_treceg_niza > i; i++)
{
do
{
scanf("%d", &niz3[i]);
} while (0 > niz3[i] || niz3[i] > 10); // citaj elemente sve dok ne ucitas cifre iz odgovarajuceg opsega
}
for (int i = 0; duzina_treceg_niza > i; i++)
printf(" %d ", niz3[i]);
printf("\n");
//pocetna vrijednost brojaca mora biti nula!
int brojac = 0;
int pomocni_brojac = 0;
// (S_1 intersect S_2) union (S_2 intersect S_3) union (S_3 intersect S_1) -- matematicko rjesenje problema
int x;
int pomocni_niz[duzina_prvog_niza + duzina_drugog_niza + duzina_treceg_niza];
for (int i = 0; duzina_prvog_niza + duzina_drugog_niza + duzina_treceg_niza > i; i++)
pomocni_niz[i] = 0;
int max;
if(duzina_prvog_niza>=duzina_drugog_niza && duzina_prvog_niza>=duzina_treceg_niza) max=duzina_prvog_niza;
if(duzina_drugog_niza>=duzina_prvog_niza && duzina_drugog_niza>=duzina_treceg_niza) max=duzina_drugog_niza;
if(duzina_treceg_niza>=duzina_drugog_niza && duzina_treceg_niza>=duzina_prvog_niza) max=duzina_treceg_niza;
//prolazimo kroz sve elemente u sva tri niza i poredimo sve elemente sa svim elementima
for (int i = 0; duzina_prvog_niza > i; i++)
{
for (int j = 0; duzina_drugog_niza > j; j++)
{
for (int k = 0; duzina_treceg_niza > k; k++)
{ // ako je element iz prvog niza jednak elementu iz drugog niza, ili je element
if (((niz1[i] == niz2[j]) && (niz2[j] == niz3[k]) && (niz1[i] == niz3[k])))
1;
else if ((niz1[i] != niz2[j]) && (niz2[j] == niz3[k]) && (niz1[i] != niz3[k]))
pomocni_niz[pomocni_brojac++] = niz2[j];
else if ((niz1[i] == niz2[j]) && (niz2[j] != niz3[k]) && (niz1[i] != niz3[k]))
pomocni_niz[pomocni_brojac++] = niz1[i];
else if ((niz1[i] != niz2[j]) && (niz2[j] != niz3[k]) && (niz1[i] == niz3[k]))
pomocni_niz[pomocni_brojac++] = niz2[j];
}
}
}
int y = 0;
for (int g = 0; pomocni_brojac > g; g++)
{
for (int l = 0; pomocni_brojac > l; l++)
{
if (pomocni_niz[g] == pomocni_niz[l])
y++;
}
if (y == 0)
brojac++;
y = 0;
}
for (int i = 0; brojac > i; i++)
printf("%d ", pomocni_niz[i]);
printf("U dva od tri niza se nalazi %d clanova.", brojac);
return 0;
}
Thanks!
There exists a pretty fast solution to your problem. You will need three more arrays each having a size of 100. Each array will record the frequency of any particular input array. The size of each frequency array is 100 since any input array will only consist of numbers in the range 0-99.
For example:
Input arrays:
A: 1 2 3 5
B: 1 2 4 6 7
C: 1 3 4 8 9 10
Frequency arrays:
0 1 2 3 4 5 6 7 8 9 10
A: 0 1 1 1 0 1 0 0 0 0 0
B: 0 1 1 0 1 0 1 1 0 0 0
C: 0 1 0 1 1 0 0 0 1 1 1
In the frequency arrays section:
The top row denotes number which may be present in any input array and the rows below contains their frequency in each input array..
Algorithm
1 : let frequencyA[100]
2 : let frequencyB[100]
3 : let frequencyC[100]
4 :
5 : for i = 0 to A.length-1
6 : if (frequencyA[A[i]] == 0) frequencyA[A[i]]++
7 : for i = 0 to B.length-1
8 : if (frequencyB[B[i]] == 0) frequencyB[B[i]]++
9 : for i = 0 to C.length-1
10: if (frequencyC[C[i]] == 0) frequencyC[C[i]]++
11:
12: for i = 0 to 99
13: if (frequencyA[i]+frequencyB[i]+frequencyC[i] == 2) Print i
The algorithm is pretty straight forward. The only that lines that deserve some explanation are mentioned below.
Line 5-10:
For each input array, we loop though each of its element and record their frequency. We record the frequency of any particular element only once, that is, if any element repeats in a single array, we will record its frequency only once. This is made sure by the if condition which checks if we have recorded the frequency of any element before or not.
Line 12-13:
We start a loop from 0 to 99 since they are the possible values of the array. In the loop, we check if sum of the frequency in the all the three frequency arrays of any element is 2 or not. If its 2, then that element is present in present exactly twice else not.
Time Complexity
The algorithm has a time complexity of O(A.length + B.length + C.length). It is a linear time complexity which is quite fast.
I can not provide you with any code as I do not code in C a lot. I hope I have helped you. If you face any trouble in understanding my answer, please do comment. I will be happy to update my answer.
For starters always use English words for identifiers. In this case your code will be readable for a larger auditorium. Otherwise it is difficult to read it.
This statement in your program
if (((niz1[i] == niz2[j]) && (niz2[j] == niz3[k]) && (niz1[i] == niz3[k])))
1;
does not make a sense.
If you need to output common elements that are present exactly in two of three arrays then there is no great sense to create a forth array with the size equal to the sum of sizes of the three arrays.
If the arrays can be unsorted and you may not sort the arrays then a straightforward approach can look the following way as it is shown in the demonstrative program below.
#include <stdio.h>
void f( const int a1[], size_t n1, const int a2[], size_t n2, const int a3[], size_t n3 )
{
size_t total = 0;
for ( size_t i1 = 0; i1 < n1; i1++ )
{
size_t count = 1;
size_t i2 = 0;
while ( i2 < n2 && a2[i2] != a1[i1] ) i2++;
count += i2 != n2;
size_t i3 = 0;
while ( i3 < n3 && a3[i3] != a1[i1] ) i3++;
count += i3 != n3;
if ( count == 2 )
{
++total;
printf( "%d ", a1[i1] );
}
}
for ( size_t i2 = 0; i2 < n2; i2++ )
{
size_t i1 = 0;
while ( i1 < n1 && a1[i1] != a2[i2] ) i1++;
if ( i1 == n1 )
{
size_t i3 = 0;
while ( i3 < n3 && a3[i3] != a2[i2] ) i3++;
if ( i3 != n3 )
{
++total;
printf( "%d ", a2[i2] );
}
}
}
if ( total != 0 ) putchar( '\n' );
printf( "%zu\n", total );
}
int main(void)
{
int a[] = { 1, 2, 3, 5 };
int b[] = { 1, 2, 4, 6, 7 };
int c[] = { 1, 3, 4, 8, 9, 10 };
size_t n1 = sizeof( a ) / sizeof( *a );
size_t n2 = sizeof( b ) / sizeof( *b );
size_t n3 = sizeof( c ) / sizeof( *c );
f( a, n1, b, n2, c, n3 );
return 0;
}
The program output is
2 3 4
3
I wrote a c program for matrix multiplication. Now I want to divide the number of rows into 5 blocks of N/5. Function should compute first block of rows in first iteration, second part in second iteration and so on. How do I specify starting row and ending row in each iteration ? I have tried it below. Could anyone help me correcting it.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, m, n, p, q, c, d, k,g, sum = 0,start=0,end=m/5;
int **first, **second, **multiply;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Value entered %d%d \n",m,n);
first = malloc(m*sizeof(int*));
for ( i =0;i <m; i++)
first[i] =malloc(n*sizeof(int));
printf("Enter the number of rows and columns of second matrix \n");
scanf("%d%d", &p,&q);
printf("value entered %d%d \n",p,q);
second = malloc(p*sizeof(int*));
for( i=0;i<p;i++)
second[i] = malloc(q*sizeof(int));
multiply = malloc(m*sizeof(int*));
for ( i=0;i<m;i++)
multiply[i] = malloc(q*sizeof(int));
printf("Enter the elements of first matrix\n");
for( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else {
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ ){
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
}
for(g=0;g<5;g++){
for ( c = start ; c < end ; c++ ) {
for ( d = 0 ; d < q ; d++ ) {
for ( k = 0 ; k < p ; k++ ) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
start=start+m/5+1;
end=end+m/5;
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ ) {
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
for ( i = 0; i < p; i++)
free(second[i]);
free(second);
for ( i = 0; i < m; i++)
free(multiply[i]);
free(multiply);
}
for ( i = 0; i < m; i++)
free(first[i]);
free(first);
return 0;
}
Start indices usually are 0 (read it always), but about ending indices,
You must save them in variables rows/cols, when you're allocating/constructing the matrix.
This is my code to find facotrial of a number and to find number of occurunces of all digits in the factorial.
#include <stdio.h>
#include <stdbool.h>
int iFactorial(int iCount)
{
int iProduct = 1;
int iNumber = 1;
while (iNumber <= iCount)
{
iProduct *= iNumber;
iNumber++;
}
return iProduct;
}
int main(void)
{
int iFac[10] = {0};
int iCount = 0;
printf("Please input a Integer: ");
scanf("%d",&iCount);
iFac[iCount] = iFactorial(iCount);
printf("\nThe value of the factorial of %d is %d\n",iCount, iFac[iCount]);
int i;
int dig[10] = {0};
while (iFac <=0)
{
int n;
n= ((iFac % 10) + 1);
dig[n] = dig[n] +1;
iFac = iFac / 10;
}
for (i = 0; i > 9; i++)
{
if (dig[i+1] >0)
{
printf ("%d %d\n", i, dig[i+1]);
}
}
}
I need to find the proper method for writing array[x] = array[x] + 1
I think following code will clear how you wanted to count the digits.
#include<stdio.h>
int factorial (int n)
{
if ( n == 1 ) return 1 ;
return n * factorial( n-1 ) ;
}
int main()
{
//Input number
int num ;
scanf( "%d", &num ) ;
//Calculate Factorial
int fact = factorial ( num ) ;
cout<< "\nFactorial of Number is " << fact ;
//Count the frequency of Digits
int dig[10] = {0} ;
while( fact )
{
int i = fact % 10 ;
dig[i]++ ;
fact /= 10 ;
}
for ( int i = 0 ; i < 10 ; i++ )
printf("\n The digit %d is present %d times " , i , dig[i] );
return 0 ;
}
Make sure you dont input large values for calculating the factorial for a number.