I'm trying to generate a random 2D array, which is filled randomly with special symbols.
Now the problem is that when I compile my code, it prints my array, but the symbols are filled from top to bottom. That is wrong, it has to start filling them from bottom. And the other problem is that each special symbol "!##$" should be filled in equally, which is determined by the number of rows of an array -> "field". So for example when an array is generated with 4 rows, then each symbol should be randomly scattered exactly 4 times.
I've been trying to fix those two issues, but I just don't know how. So I'd be very thankfull for someone, who could point me towards fix and help me understand where my mistakes are.
My code :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int main() {
srand(time(NULL));
int y, x, a=0, b=0;
y = (rand()%(8-3+1)+3); // y = number of rows
x = (rand()%(8-4+1)+4); // x = number of columns
char field[y][x];
const char* symbol = "!##$";
int r; // determines how many symbols are used based on
if(x == 7) // number of columns - 2 = number of symbols used
r = x-3;
else if(x > 6)
r = x/2;
else
r = x-2;
int randnum1 = (rand()%x), randnum2 = (rand()%x);
if(randnum1 == randnum2) { // takes 2 random indexes from number of columns
randnum2 += 1; // and fills them with blanks
}
if(randnum2 >= x) {
randnum2 -= 2;
}
int num1 = 0, num2 = 0, num3 = 0, num4 =0; // checks if same number of all
// symbols is used
for(a = 0; a < y; a++) {
printf("\n %d |", a+1);
for(b = 0; b < x; b++) {
if(b == randnum1 || b == randnum2) {
field[a][b] = ' ';
printf(" %c |", field[a][b]);
}
else {
field[a][b] = symbol[rand()%r];
int sum1 = num1+num2+num3+num4; // sum checks if the total max
int sum2 = y * r; // number of symbols has been used
if(field[a][b] == '!')
num1 += 1;
if(field[a][b] == '#')
num2 += 1;
if(field[a][b] == '#')
num3 += 1;
if(field[a][b] == '$')
num4 += 1;
if(sum1 < sum2) {
if((num1 < y) || (num2 < y) || (num3 < y) || (num4 < y)) {
printf(" %c |", field[a][b]);
}
else {
field[a][b] = ' ';
printf(" %c |", field[a][b]);
}
}
else {
field[a][b] = ' ';
printf(" %c |", field[a][b]);
}
}
}
}
putchar('\n');
for(a = 0; a < 1; a++) {
printf(" ");
for(b = 0; b < x; b++) {
printf("--- ");
}
}
putchar('\n');
for(b = 0; b < 1; b++) {
printf(" ");
for(b = 0; b < x; b++) {
printf(" %d ", b+1);
}
}
putchar('\n');
return 0;
}
Related
I have an assignment to write a program for a natural number where its inverse is divisible by its number of digits. A natural number n ( n > 9) is entered from the keyboard. To find and print the largest natural number less than n that its inverse is divisible by its number of digits. If the entered number is not valid, print a corresponding message (Brojot ne e validen).
I have tried :
#include <stdio.h>
int main() {
int n,r,s=0,a=0;
int m;
scanf("%d",&n);
int t=n;
if(t<10)
{ printf("Brojot ne e validen");}
else {
for (int i = n - 1; i > 0; i--) {
while (n != 0) {
r = n % 10;
s = (s * 10) + r;
n = n / 10;
a++;
if (s % a == 0) {
m = i;
break;
}
}
}
printf("%d\n", m);
}
return 0;
}
And when my inputs is 50, it gives the correct answer which is 49, but when I try numbers like 100 or 17 it prints 98 instead of 89 and 16 instead of 7 respectively. I have been baffled by this for more than an hour now
check your logic.
you can check each value by
#include <stdio.h>
int main() {
int t,r,s=0,a=0;
int m;
scanf("%d",&t);
if(t<10)
{ printf("Brojot ne e validen");}
else {
for (int i = t - 1; i > 0; i--) {
while (t != 0) {
r = t % 10;
printf("%d \n", r);
s = (s * 10) + r;
printf("%d \n", s);
t = t / 10;
printf("%d \n", t);
a++;
if (s % a == 0) {
m = i;
break;
}
}
}
printf("%d\n", m);
}
return 0;
}
In line 10 I cannot find out where my problem is at first. I place int a[100][100]={0} but the cpu speed is stuck.
Then, I try to change it into a[n][n] but no output is shown.
Last, I try to change it again as if it resembles the original ones.
However, nothing works instead of a new question.
#include<stdio.h>
int main() {
int n;
while (scanf("%d", &n)) {
n *= 2;
int x = 0, y = 0, num = 1;
int a[n][n] = {0};
a[x][y] = num++;
while (n * n >= num) //定義陣列
{
while (y + 1 < n && !a[x][y + 1]) //向右
a[x][++y] = num++;
while (x + 1 < n && !a[x + 1][y]) //向下
a[++x][y] = num++;
while (y - 1 >= 0 && !a[x][y - 1]) //向左
a[x][--y] = num++;
while (x - 1 >= 0 && !a[x - 1][y]) //向上
a[--x][y] = num++;
}
for (x = 0; x < n; x++) //print 陣列
{
for (y = 0; y < n; y++) {
if (y != n - 1) {
printf("%d ", a[x][y]);
} else {
printf("%d", a[x][y]);
}
}
printf("\n");
}
break;
}
return 0;
}
At least this problem:
Variable Length Arrays (VLA) cannot be initialized via the C standard.
Alternate, assign via memset() after defining a.
// int a[n][n]={0};
int a[n][n];
memset(a, 0, sizeof a);
I am trying to create a program in C to check if the lines of rectilinear polygon intersect each other at any point.
I only need simple rectilinear polygons that do not intersect at any point. It can be counter-clockwise or clockwise.
The direction values will be less than 10. NS direction must alternate with WE direction and vice versa.
The inputs being passed are in the form of direction from an input file such as; and also shown in the pictures:
S 2 E 4 S 2 E 4 N 2 W 4 N 2 W 4
I tried to store points in a two-dimensional array with every point being checked as true, but I am unable to figure out how to move in case of counter and clockwise direction as the points could be N4 E6 or S4 W6. In such a case if I add when the value is N-4 (x,y) = (0,4) and subtract when S-4 (x,y) = (0,-4) that would fail when using it as index in array.
int arr[10][10];
int xPrime = 0, yPrime = 0;
bool checkContinuity(int y, const char * dir ){
if(strcmp(dir, "S")==0){
y = -y;
cols = y;
int j;
for(j = cols; j >= 0; j--){
if(arr[xPrime][j] == 1 && j != yPrime){
return false;
}
arr[xPrime][j] = 1;
printf(" %d ", j);
}
yPrime -= y;
if(yPrime < 0)
yPrime = -yPrime;
}
else if(strcmp(dir, "W")==0){
y = -y;
cols = y;
int j;
for(j = cols; j >= 0; j--){
if(arr[j][yPrime] == 1 && j != xPrime && (j != 0 && yPrime != 0)){
return false;
}
arr[j][yPrime] = 1;
printf(" %d ", j);
}
xPrime -= y;
if(xPrime < 0)
xPrime = -xPrime;
}
else if(strcmp(dir, "N")==0){
cols = y;
int j;
for(j = 0; j <= cols; j++){
if(arr[xPrime][j] == 1)
return false;
arr[xPrime][j] = 1;
printf(" %d ", j);
}
yPrime += y;
}
else if(strcmp(dir, "E")==0){
cols = y;
int j;
for(j = 0; j <= cols; j++){
if(arr[j][yPrime] == 1 && j != xPrime)
return false;
arr[j][yPrime] = 1;
printf(" %d ", j);
}
xPrime += y;
}
else
return false;
return true;
}
It might be easier to store the actual polygon rather than all possible
points of the plane. We are then not restricted by the choice of the
point array (arr in your code). See this working example:
the polygon is stored in the int-array P = {0, 0, x1, y1, x2, y2, ...}
segments are two-point polygons. The function 'intersection' checks if
two such segments Q and P intersect;
if so, returns the intersection coordinates.
It uses the helper function 'between' to ckeck if one number is between two others.
the function 'next' calculates the next point of the polygon, assuming
the input is provided as char-string (eg: "S2E4S2E4N2W4N2W4")
In the function main we now loop over all segments, and check if they intersect with
any previous one.
Of course, the input should at some point be checked for sanity etc.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void
next( const char *s,
int *v )
{
v[2] = v[0];
v[3] = v[1];
int step = s[1] - '0';
switch(s[0]) {
case 'S': v[3] -= step; break;
case 'N': v[3] += step; break;
case 'W': v[2] -= step; break;
case 'E': v[2] += step; break;
}
}
int
between( int x,
int a,
int b )
{
return a < b ? x >= a && x <= b : x >= b && x <= a;
}
int
intersection( int *P,
int *Q,
int *R )
{
if(P[0] == P[2] && Q[1] == Q[3]){ // P vertical, Q horizontal (w.l.o.g.)
if(between(P[0], Q[0], Q[2]) && between(Q[1], P[1], P[3])){
R[0] = P[0];
R[1] = Q[1];
return 1;
} else
return 0;
}else if(Q[0] == Q[2] && P[1] == P[3])
return intersection(Q, P, R);
else return 0;
}
int
main() {
char *s = "S2E4S2E4N2W4N2W4";
int n = strlen(s) / 2, // number of steps
*P = calloc((n + 1) * 2, sizeof(int)), // polygon
R[2]; // intersection
if(!P) exit(137);
for(int k = 0; k < n; k++){
next(s + 2 * k, P + 2 * k);
for(int j = 0; j < k - 1; j++) {
if(intersection(P + k * 2, P + j * 2, R)) {
printf("Intersection at: %d, %d\n", R[0], R[1]);
exit(0);
}
}
}
printf("No intersection\n");
}
As you see on the image there are no * in lower part of the circle. Why is that?
Equations in the loops: y = 10 - i , x = j-10 or k-10
Circle formula = r^2 = (x-a)^2 + (y-b)^2
int i, j, k;
for (i = 0; i < 21; i++) {
if (i == 10) {
for (j = 0; j < 21; j++) {
if (pow(r * r - (j -10 -a)*(j -10 -a), 0.5) + b == 0) {
printf("*");
}
else if (j == 10) {
printf("|");
}
else {
printf("-");
}
}
}
else {
for (k = 0; k < 21; k++) {
if (pow(r * r - (k -10 -a)*(k -10 -a), 0.5) + b == 10 - i) {
printf("*");
}
else if (k == 10) {
printf("|");
}
else {
printf(" ");
}
}
}
printf("\n");
}
enter image description here
You program should be more like this:
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k;
int a = 3, b = 4, r = 5;
int x, y;
for (i = 0; i < 21; i++) {
y = -(i - 10);
if (y == 0) {
for (j = 0; j < 21; j++) {
x = j - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf("-");
}
}
}
else {
for (k = 0; k < 21; k++) {
x = k - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(" ");
}
}
}
printf("\n");
}
}
When you match your equation, you should allow for (-3) ^ 2 is 9, while if you take the square root of 9, you could only match a 3, unless if you also check whether the "minus" of the square root is a 3, which is troublesome.
It might also be good that if you use x and y and do the calculations, and then at the last moment, map to the screen's method of displaying it, or a similar way, instead of using i and then -(i - 10) or 10 - i every where to mean y, so I added the x and y in your program. You might also comment that (a, b) is supposed to be the center of the circle.
There is also one point about, it seems the difference between y being 0 or not is that you print out the hyphen (for the x-axis), vs a space, so your program could be:
#include <stdio.h>
#include <math.h>
int main() {
int i, j;
int a = 3, b = 4, r = 5;
int x, y;
for (i = 0; i < 21; i++) {
y = -(i - 10);
for (j = 0; j < 21; j++) {
x = j - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(y == 0 ? "-" : " ");
}
}
printf("\n");
}
}
and in fact, you don't need i and j, but can directly iterate through x and y, as your way of displaying is only one character each time:
#include <stdio.h>
#include <math.h>
int main() {
int a = 3, b = 4, r = 5;
int x, y;
for (y = 10; y >= -10; y--) {
for (x = -10; x <= 10; x++) {
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(y == 0 ? "-" : " ");
}
}
printf("\n");
}
}
I have a code that finds the sum of the divisors of a number, but I can't get it to apply on my increasing n and print all the numbers respectively.
The code is
long div(int n) {
long sum = 0;
int square_root = sqrt(n);
for (int i = 1; i <= square_root; i++) {
if (n % i == 0) {
sum += i;
if (i * i != n) {
sum += n / i;
}
}
}
return sum - n;
}
On my main() I need to have a c number that starts from 1 and goes to my MAXCYC which is 28. The n goes from 2 to MAXNUM which is 10000000. The program needs to find all perfect, amicable and sociable numbers and print them with their respective pairs.
Sample output:
Cycle of length 2: 12285 14595 12285
Cycle of length 5: 12496 14288 15472 14536 14264 12496
for (int n = 2; n <= MAXNUM; n++) {
long sum = div(n);
long res = div(sum);
if (res <= MAXNUM) { // Checking if the number is just sociable
int c = 0;
while (c <= MAXCYC && n != res) {
res = div(sum);
c++;
}
if (c <= MAXCYC) {
printf("Cycle of length %d: ", c);
printf("%ld ", sum);
do {
printf("%ld ", res);
res = div(res);
}
while (sum < res);
printf("%ld ", sum);
c += c - 2;
printf("\n");
}
}
}
I only get pairs of cycle length of 1, 2 and nothing above that. Also it doesn't even print it correctly since it says Cycle of length 0: in all of the results without increasing. I think the problem is in the f before the first print but I can't get it to work in a way that as long as my
(n == sum) it prints Cycle of length 1: x x pairs
(n == res && sum < res) it prints Cycle of length 2: x y x pairs
(res <= MAXNUM) it prints Cycle of length c: x y z ... x (c amount of pairs including first x)
What do you guys think I should change?
Ok, this code should work if I understood well your requirement.
#include <stdio.h>
#include <stdlib.h>
int div_sum(int n)
{
long sum = 0;
int square_root = sqrt(n);
for (int i = 1; i <= square_root; i++)
{
if (n % i == 0)
{
sum += i;
if (i * i != n)
{
sum += n / i;
}
}
}
return sum - n;
}
int MAX_N = 10000000;
int MAX_CYCLES = 28;
int main()
{
int cycles;
for(int n = 2; n < MAX_N; n++){
int found = 0;
for(int c = 1; !found && c <= MAX_CYCLES; c++){
cycles = c;
int aliquote = n;
while(cycles--) aliquote = div_sum(aliquote);
//it is a cycle of length c
cycles = c;
if(n == aliquote){
printf("Cycle of length %d: %d", c, n);
while(cycles--){
aliquote = div_sum(aliquote);
printf(" %d", aliquote);
}
printf("\n");
found = 1;
}
}
}
return 0;
}