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.
Related
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);
}
}
#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.
I need to input five numbers and then a positive or negative. The order will be changed like the number.
For example, if three (3) is the number then:
1 2 3 4 5 6 7 8 9 10
will become:
4 5 6 7 8 9 10 1 2 3
Please do not use pointers. I have made the following code and would like to be able to improve on it. Can I use a mathematical formula using the modulus operator % ? If so, how would I be able to do it.
Thanks, this is my code.
#include<stdio.h>
#define n 10
int main(void)
{
int num[n] = { 0 }, assist[n] = { 0 }, i = 0, j = 0, variable = 0, k = 0;
for (i = 0; i < n; i++)
{
printf("please enter index. %d\n", i );
scanf("%d", &num[i]);
}
printf("please enter the circle number\n");
scanf("%d", &variable);
printf("\n\n");
if (variable >= 0)
{
for (i = variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < variable, j < n; i++, j++)//to assist//
{
assist[j] = num[i];
}
}
if(variable < 0)
{
for (i = n + variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < n + variable, j < n; i++, j++)
{
assist[j] = num[i];
}
}
for (i = 0; i < n; i++)//output//
{
printf("%d\n", assist[i]);
}
return 0;
}
Something like this. I did not compile; any small mistakes are for you.
#include<stdio.h>
#define N 10
int main(void)
{
int numbers[N];
int offset;
for (int i = 0; i < N; i++)
{
printf("please enter number %d: ", i);
scanf("%d", &num[i]);
}
printf("\nplease enter the circle number: ");
scanf("%d", &offset);
printf("\n\n");
for (int i = 0; i < N; i++)
{
printf("%d ", num[(i + offset) % N]);
}
printf("\n");
}
The title is pretty clear I think.
I am trying to create a program that calculates a 3x3 linear system using determinants, but I am getting a segmentation fault. Here is the code:
#include<stdio.h>
int determinant(int n, int m, int det[m][n])
{
int res;
res = det[0][0]*det[1][1] - det[0][1]*det[1][0];
return res;
}
int main(void)
{
int arr[3][4], det[2][2], i, j, D; //Dx1, Dx2, Dx3
for(i = 0; i < 3; i++)
{
printf("Eisagete tous suntelestes ths %dhs eksisoshs.", i+1);
scanf("%d %d %d %d", &arr[i][0], &arr[i][1], &arr[i][2], &arr[i][3]);
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+1];
}
}
D = arr[0][0]*determinant(2, 2, det);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+((j == 1) ? 1 : 0)];
}
}
D -= arr[0][1]*determinant(2, 2, det);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j];
}
}
D += arr[0][2]*determinant(2, 2, det);
printf("%d\n", D);
}
I am getting the error right after completing the first for loop in main.
In the block:
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+1];
}
}
You increment i in both loops, and adding 1 more to it while reading from the array. So at arr[i+1] you are reading to far.
A segmentation fault basically means you are trying to read something you don't have access to.
You shoud never do what you're doing by passing static array sizes m and n as function argument:
int determinant(int n, int m, int det[m][n])
Check https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html for info
At the moment I'm wondering how can I swap the values already in a two dimensional array, in this case I managed to swap them, however, I can't seem to be able to swap them back... How can I make a program swap the values back and forward for the array?
Code:
#include <stdio.h>
void main()
{
//declaration of values
int d = 0;
char value;
float list[5][6], swaplist[5][6];
float average_odd, sum = 0, average_even, sum1 = 0;
//Command list so that the user knows what he can do in this program
printf("Command list:\t \n\nCommand: \t\t Output: ");
printf("\n \"A\" \t\t Declare values of a list.\n \"O\" \t\t Obtain the average value of the even and odd column\n\t\t values in the list.\n");
printf(" \"I\" \t\t Exchange the values on the even columns with the odd ones.\n \"P\" \t\t Print the values of the list.\n \"S\" \t\t End program.");
//========================================================================================================
while (d != 1)
{
printf("\n\nInsert value: ");
scanf(" %c", &value);
//=========================================================================================================
if (value == 'a' || value == 'A')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("Insert value of element %d in column %d: ", i + 1, j + 1);
scanf("%f", &list[i][j]);
swaplist [i][j] = list[i][j];
}
}
}
//=========================================================================================================
if (value == 's' || value == 'S')
{
d++;
}
//=========================================================================================================
if (value == 'P' || value == 'p')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("%2.2f ", list[i][j]);
}
printf("\n");
}
}
//========================================================================================================
if (value == 'o' || value == 'O')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
sum += list[i][j];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
sum1 += list[i][j];
}
}
average_even = sum1 / 15;
printf("The average of the even columns = %.2f\n", average_even);
average_odd = sum / 15;
printf("The average of the odd columns = %.2f\n", average_odd);
}
//=======================================================================================================
if (value == 'i' || value == 'I')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j+1];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j - 1];
}
}
}
}
}
After swapping, you don't change the state of the swap list. At first, the swap list is a copy of the unswapped matrix. You don't change it to reflect the changes in the matrix and don't keep track of any other state.
You don't really need the swap list. I suggest that you just do a regular swapping of adjacent columns cell by cell:
if (value == 'i' || value == 'I') {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j = j + 2) {
float swap = list[i][j];
list[i][j] = list[i][j + 1];
list[i][j + 1] = swap;
}
}
}
That will toggle the matrix on repeated swaps.