The code fits the first number and prints it constantly. how can i fix this?
int count = 0;
for (int i = 0; i <= 20; i++) {
for (count = 2; i > 1; count++) {
while (i % count == 0) {
printf("%d ", count);
i = i / count;
}
}
}
The values in each iteration are as follows.
count = 0; i = 0; Doesn't enter the second for.
count = 0; i = 1; Doesn't enter the second for.
count = 0; i = 2; Enters the second for. count = 2;
2 % 2 == 0 - Enters the while.
i = 2 / 2; 1 % 2 == 1; Doesn't enter the while.
Back to the second for - count = 3;, i = 1; Doesn't enter the second for.
Back to the first for - i < 20;, so i = 2.
count = 2; i = 2; and we are back to step 4, with an infinite loop.
This might be what you are looking for -
int j, count = 0;
for (int i = 20; i > 0; i--)
{
printf("\n%d: ", i);
for(count = 2, j = i; j > 1; count++)
{
while(j % count == 0)
{
printf("%d ", count);
j = j / count;
}
}
}
Define a function that checks whether a given number n is prime:
bool is_prime(int n)
{
if (n < 2) return false;
for (int i = 2; i <= n/i; ++i) // Doing i*i<=n may overflow
if (n % i == 0) return false;
return true;
}
And then call it like:
for (int i = 0; i <= 20; i++)
if(is_prime(i))
printf("%d\n", i);
Or more directly (i.e. without a function):
int main(void)
{
int mark;
for (int n = 2; n <= 20; n++) {
mark = 1;
for (int i = 2; i*i <= n; ++i)
if (n % i == 0) mark = 0;
if (mark) printf("%d\n", n);
}
}
Related
This loop checks the previous element in an array. The question is how can it be avoided to check the arr[0][0] with its previous element which causes undefined behavior?
Here is the code so far but it has this issue with the the first element being checked with its previous element.
int main()
{
int arr[2][4];
int k, n;
for (k = 0; k < 2; k++) {
for (n = 0; n < 4; n++) {
do {
printf("Provide a number");
scanf("%d", &arr[k][n]);
printf("This is %d in the position[%d][%d]\n", arr[k][n], k, n);
if (n==0) break;
printf("The arr[k][n] is %d and the arr[k][n-1] is %d and n-1 means %d\n", arr[k][n], arr[k][n - 1], n - 1);
} while (arr[k][n] <= arr[k][n - 1]); //Here is the issue
}
}
for (k = 0; k < 2; k++) {
for (n = 0; n < 4; n++) {
printf("%d\n", arr[k][n]);
}
}
}
The issue:
Adding the if (n==0) break; causes the program to allow adding smaller numbers than the ones inserted so far. While not including this line causes undefined behavior. How can this be fixed?
This is how it works now, which is not correct:
2 3 4 5
2 4 5 6
The printf statements are only for the purpose of viewing what is going on.
You only check with previous column. That is not related to your break but due to broken logic.
You could do it like this:
int main()
{
int arr[2][4];
int k, n;
int last_number, new_number;
for (k = 0; k < 2; k++) {
for (n = 0; n < 4; n++) {
do {
printf("Provide a number");
scanf("%d", &new_number);
printf("This is %d in the position[%d][%d]\n", new_number, k, n);
if (n==0 && k == 0)
break; // Don't check for increasing values.
} while (new_number < last_number);
arr[k][n] = new_number;
last_number = new_number;
}
}
for (k = 0; k < 2; k++) {
for (n = 0; n < 4; n++) {
printf("%d\n", arr[k][n]);
}
}
}
Change
while (arr[k][n] <= arr[k][n - 1]);
to
while (n > 0 && arr[k][n] <= arr[k][n - 1]);
This works because && short circuits the test when n == 0 and does not do the second test.
You will also need to fix the print statement.
#include<stdio.h>
#include<time.h>
int main(void)
{
srand(time(NULL));
int answer;
int treatment = rand() % 4;
printf("###발모제 찾기###\n\n");
int cntShowBottle = 0;
int prevCntShowBottle = 0;
for (int i = 1; i <=3; i++)
{
int bottle[4] = { 0, 0, 0, 0 };
int* ptr = bottle;
do {
cntShowBottle = rand() % 2 + 2;
} while (cntShowBottle == prevCntShowBottle);
prevCntShowBottle == cntShowBottle;
int isincluded = 0;
printf(" %d 번째 시도 : ", i);
for (int j = 0; j < cntShowBottle; j++)
{
int randBottle = rand() % 4;
if (bottle[randBottle] == 0)
{
bottle[randBottle] = 1;
if (randBottle == treatment)
{
isincluded = 1;
}
}
else
{
j--;
}
}
I want to get different array 'bottle[]' every time like {0,1,1,0},{1,1,1,0},{1,1,0,1} without duplication.. I think it needs (do while) and (pointer), but I don't know how to coding.
Im trying to submit a solution for the problem FCTRL2(https://www.codechef.com/problems/FCTRL2) on codechef. On executing the code it sometimes gets executed successfully while sometimes it gives a SIGSEGV error. But when I submit it, it always shows wrong answer. Though the code gives correct answer when i run it on any other IDE.
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
int testCases, i, j, k, n, num, digits, carry = 0, temp;
scanf("%d", &testCases);
int testArr[160];
for (i = 0; i < testCases; i++)
{
scanf("%d", &n);
num = n;
if (n == 0 || n == 1)
{
testArr[0] = 1;
digits = 1;
}
else
{
k = 0;
for (j = 10; n != 0; j = j * 10)
{
testArr[k] = n % j;
n = n / j;
k++;
}
digits = k;
for (j = 1; j < num; j++)
{
for (k = 0; k < digits; k++)
{
temp = testArr[k] * j + carry;
if (temp > 10)
{
testArr[k] = temp % 10;
carry = temp / 10;
}
else
{
testArr[k] = temp;
carry = 0;
}
}
if (carry > 10)
{
testArr[k] = carry % 10;
k++;
testArr[k] = carry / 10;
digits = k + 1;
carry = 0;
}
else if (carry > 0)
{
testArr[k] = carry;
digits = k + 1;
carry = 0;
}
}
}
for (k = (digits - 1); k >= 0; k--)
{
printf("%d", testArr[k]);
}
printf("\n");
}
return 0;
}
The code that I'm writing looks fine and provides no compilation errors or segmentation faults, but it provides completely random numbers in the output file.
int reroll()
{
return rand() % 5 + 2; // range of 2-6
}
int roll()
{
int i;
int rolls[4];
int sum = 0, low = 6;
for(i = 0; i < 4; i++)
{
rolls[i] = rand() % 6 + 1; // range of 1-6
while(rolls[i] == 1)
{
rolls[i] = reroll();
}
}
for (i = 0; i < 4; i++)
{
if (low > rolls[i])
low = rolls[i];
}
for (i = 0; i < 4; i++)
{
sum += rolls[i];
}
sum = sum - low;
return sum;
}
void abScores()
{
int i, j, choice, abSc[6], abil[6];
char abS[6][5] = {"Str", "Dex", "Con", "Int", "Wis", "Cha"};
FILE *fp;
FILE *fo;
fp = fopen("csheet.txt", "a");
fo = fopen("att.txt", "r");
printf("Would you like your own ability scores, or the basic ability scores?\n");
printf("1 for own, 2 for basic\n");
scanf("%d", &choice);
if (choice == 1)
{
for (i = 0; i < 6; i++)
{
abSc[i] = roll();
printf("Ability score %d: %d\n", i+1, abSc[i]);
}
}
else if (choice == 2)
{
abSc[0] = 15;
abSc[1] = 14;
abSc[2] = 13;
abSc[3] = 12;
abSc[4] = 10;
abSc[5] = 8;
for (i = 0; i < 6; i++)
printf("Ability score %d: %d\n", i+1, abSc[i]);
}
for (i = 0; i < 6; i++)
{
printf("What score would you like for %s?\nPlease use each score only once.\n", abS[i]);
for (j = 0; j < 6; j++)
{
printf("%2d\n", abSc[j]);
}
scanf("%d", &abil[j]);
system("cls");
}
for (i = 0; i < 6; i++)
fprintf(fp, "%s: %d\n", abS[i], abil[i]);
}
I'm trying to make a rather basic D&D Character Generator, but this function, abScores, is causing me more problems than the other 11 functions combined.
I'm attempting to print out the prime numbers up to a certain value that is obtained from the user. I imagine there is something wrong with my for loop if I only ever receive an answer of 1?
#include <stdio.h>
#include <cs50.h>
int main (void)
{
printf("Length: ");
int length = GetInt();
bool notPrime = false;
for (int i = 1; i < length; i++)
{
for (int k = 1; k <= i/2; k++)
{
if (i % k == 0)
{
notPrime = true;
break;
}
else
{
notPrime = false;
}
}
if (notPrime == false)
{
printf("%d ", i);
}
}
printf("\n");
}
In the internal loop:
for (int k = 1; k <= i/2; k++)
You are starting with k = 1 and testing if k divides i. 1 divides any integer, so the answer will always be "non prime", which is not the case (remember the definition of prime number). Start from 2:
for (int k = 2; k <= i/2; k++)
in the internal loop:
for(k=2; k<=sqrt(i); k++)
will work.
You may have to check the special cases and you can start from 3.
Also you can increment by 2 exluding the pair numbers:
for(int i = 1; i < length; i++){
if(number < 2) prime = true;
if(number == 2) prime = false;
if(number % 2 == 0) prime = false;
for (int k = 3; k <= i/2; k++){
if(number % i == 0 ){
prime = false;
break;
}
}
if (prime){
printf("%d ", i);
}
}
You should make the IF's out of the first bucle, and also you can change k <= i/2 -> sqrt(i)