Simple Pyramid code equivalent matrix with coordinates - c

I am having a little difficult time, making the inverted pyramid of stars code of C/CPP equivalent code on dart/flutter for a game. Specially about empty spaces calculation.
I need to show objects in place of stars. So I need to find/calculate their coordinates in a list or matrix. I need some help correcting this.
Update: After some correction, It is printing to cooridantes that are putting objects far off.
code:
int number = 10;
StringBuffer stringBuffer = StringBuffer();
List<Vector2> vectors = [];
double objectSize = 100;
int temp = 0;
for (int i=number; i >= 1; i--)
{
for (int j=number; j > i; j--)
{
stringBuffer.write("-");
temp = j*objectSize;
}
for (int k=1; k < i*2; k++)
{
stringBuffer.write("*");
vectors.add(Vector2(objectSize*k +temp, objectSize*i));
}
stringBuffer.write("\n");
}
print(stringBuffer.toString());
print(vectors.toString());
Coordinates:

Related

Comparing two arrays..in c

I set up two arrays one of 50 units big, array1[50] which has 50 random integers from a range of 50-100 and another array which prompts the user to enter in 10 intgers from the same range of numbers. My problem is, how do i compare the two, im tyring to find the number of times the 10 user inputted numbers match the numbers stored in the array1[50] which holds the seedeed random numbers. Ive tried to do a for loop within a for loop, like this.
array2[10]
array[50]
int counter = 0;
for(int i = 0; i < 10; ++i){
for(int k = 0; k < 50; ++k){ //i've tried this and it does not work, i don't know what else to do
if( array2[i] == array[k]);
++counter;
}
}
//any help is appreciated thanks.
There are easier ways to achieve this using other libraries. But considering you're working with small arrays, you could use your code with the following modification:
array2[10]
array[50]
int counter = 0;
for(int i = 0; i < 10; ++i){
for(int k = 0; k < 50; ++k){
if( array2[i] == array[k])counter++;
}
}
That should work.

C programming: Generate random n-combinations from given set?

Say I have a pre-specified set S of m items. I would like to generate a random combination of n (unique) items taken from S.
Is there an easy way to implement this in C? I looked into rand() but it didn't seem to do what I want.
(EDIT to add more details)
The specific problem is to randomly choose n distinct elements from an array of size m. My first instinct is to do this:
idx_array = []
int idx = rand() % m
[if idx not in idx_array, add to idx_array. Otherwise repeat above line. Repeat until idx_array has size n]
But it doesn't look like this process is truly random. I'm still new to C and really just want to know if there's a built-in function for this purpose.
Any help appreciated.
Instead of generating a number from 1 to n with the possibility of duplicate, shuffle your array and then pick out of the first n elements:
#include <stdio.h>
#include <stdlib.h>
// Randomly shuffle a array
void shuffle (int * array, int n) {
int i, j, tmp;
for (i = n - 1; i > 0; i--) {
j = arc4random_uniform(i + 1);
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
int main (int argc, char const *argv[])
{
const int m = 5;
const int n = 3;
int s[m] = {10, 20, 30, 40, 50};
// Make a copy of s before shuffling it
int t[m];
for(size_t i = 0; i < m; i++)
{
t[i] = s[i];
}
shuffle(t, m);
// Now, the first n elements of t is what you want
for(size_t i = 0; i < n; i++)
{
printf("%d ", t[i]);
}
return 0;
}
Credit to Roland Illig for the Fisher-Yate shuffling function.
This is a sampling problem. There are a host of sampling algorithms but a straightforward algorithm which does the job pretty well is known as Reservoir Sampling. Refer geekforgeeks for more details on reservoir sampling.

A way to go over the diagonals of an array of size [6][7]

I'm building in C language, a game called 4-in-a-row or Connect Four, for a fast review of the game you can see here:
http://en.wikipedia.org/wiki/Connect_Four
so, I have a 2 dimensional array of size [6][7], and I want to check in diagonal if there are 4 tokens which are "*" or "o" that are defined as a chars which are in a a row. I'm trying to write a function that after each play, it sums up all the possible diagonals and see if the sum is 4 for example, or if we want to check in pairs, if we get three similar pairs then there are 4 equal tokens in a row, so in this case the sum is 3, and so on..
for all I know, there are 12 different different diagonals (every 6 on different direction), how do u suggest me to write this function while being the most effective? and also including all the possibilities with less that 16 lines of code.
any kind of help would be appreciated!
here is an example of what I did:
int CheckDiagonal_1(char matrix[Rows][Columns])
{
int s_count = 0;
int o_count = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 5; j >= 3; j--)
{
for(int k = 0; k <= 3; k++)
{
if(matrix[j-k][i+k]== matrix[j-k-1][i+k+1]) count ++;
if(count==4) return count;
}
count = 0;
}
}
return 0;
}
Diagonals are sequences where
i == j + c for i from (0,height) and c (-width, height)
or i == -j + c.
So if goal to write code that fits into small number of lines - just write loops that go over i {0-6} and check for indexes to fit in range. Something like
for (int c= -7; c < 7; c++)
{
int starsOnDiag = 0;
for(int i = 0; i < 7; i++)
{
starsOnDiag += !indexesInRange(i, j) ? 0 :
cell[i, i+c] == '*' ? 1 : 0;
}
... // other diagonal and check for other symbol
}

Sort function failing for larger arrays

I'm writing a program to implement Prim's Algorithm for minimum spanning trees for a short project for a course. The first step is to sort the edges according to weight; the code I have for this works sometimes, but not always.
Here is the code:
for(int i = 0; i < graph.edges; ++i)
{
least_remain_edge = i;
for(int k=i+1; k<graph.edges; ++k)
{
if(graph.edge[k][3]<graph.edge[least_remain_edge][3])
{
least_remain_edge = k;
}
}
if(least_remain_edge != i)
{
swap_temp = graph.edge[i][0];
graph.edge[i][0] = graph.edge[least_remain_edge][0];
graph.edge[least_remain_edge][0] = swap_temp;
}
}
graph.edge[i][3] is the weight of the ith edge, and [i][0] is the edges reference/name. It's something like a bubble sort, where it finds the smallest in the remainder of the list, and puts it in the ith place. I can't see why this isn't always working!
When you're moving around elements, you're only moving their name/reference around, and not the weights and whatever else you're storing. So, maybe do something like
for (int k = 0; k < 4; k++) {
swap_temp = graph.edge[i][k];
graph.edge[i][k] = graph.edge[least_remain_edge][k];
graph.edge[least_remain_edge][k] = swap_temp;
}

C: Problem with initializing array values to zero

I'm trying to construct the following 14*14 array i C: [I 0; 0 -I], that is a 7*7 identity matrix upper left, minus the identity lower right and zeros otherwise.
This is the method:
#define DIM 7
double S[2*DIM][2*DIM];
for(i = 0; i < DIM; i++){
for(j = 0; j < DIM; j++){
if(i == j){
S[i][j] = 1.0;
S[i+7][j+7] = -1.0;
}
else{
S[i][j] = 0.0;
}
}
}
This works fine for all the diagonal elements; however, some elements of the array get initialized to crazy values; for example, 13,6 gets initialized to
68111186113812079535019899599437200576833320031036694798491976301968333351950125611739840800974137748034248687763243996679617222196278187875968953700681881752083957666277350377710107236511681624408064.000000
This seems to be happening consistently (at least thrice) to entries 11,13, 12,9, 12,10, 13,12 and 13,6.
Can anybody tell me what's at play here or provide an alternative solution?
Cheers!
EDIT: The weird entries aren't consistent.
EDIT2: Typo: 13,12, not 13,15
Your loop only covers the upper left quadrant, so the off-diagonal elements in the other quadrants are not initialized and contain garbage. Your loop should go up to 2*DIM for each dimension, so that the off-diagonal elements are zeroed, and then your conditional for the diagonal elements should just be a little more complex to decide which value to set the diagonal element to.
Note that [13, 15] is entirely outside of this array!
You can initialize the whole array with zeros, then set only the diagonal
double S[2*DIM][2*DIM] = {0};
for (i = 0; i < DIM; i++) {
s[i][i] = 1;
s[i + DIM][i + DIM] = -1;
}
You are never writing to s[i][j] for i != j and i >= DIM or j >= DIM. If your array has automatic storage (is "local") it contains arbitrary init values.
I would say most of the elements that are outside 7x7 will not be initialized at all unless i == j (diagonal elements).
What do you want to initialize them to?
That's because your not initializing those elements. Here is some better code:
#define DIM 14
double S[DIM][DIM];
for (i = 0; i < DIM; i++) {
for (j = 0; j < DIM; j++) {
if (i == j) {
if (i < 7) {
S[i][j] = 1.0;
} else {
S[i][j] = -1.0;
}
} else {
S[i][j] = 0.0;
}
}
}
You never initialize values with an i or j between DIM + 1 and 2*DIM. So when you look at a value stored in one of those positions, you see whatever was there before that space was accessed by your matrix.

Resources