Checking for numbers within arrays in C - arrays

The function below is looking through Order1-3 and looks if any of the elements are within each of the 3 column values of each of the 8 rows inside Winning_order. So in this case Order1 values are within the first row of Winning_order so it comes out as true as well as Order2. However Order2 is not a valid output for the program, how can I modify the iterating function below so that it checks to be true. Order3 is meant to be a false as well since its not within Winning_order. The code has been gotten from the answer of this issue issue.
#include <stdio.h>
// Iterating function
int match_arrays(int *arr1, int *arr2, int len)
{
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
// Main function
int main(void)
{
int Order1[3] = {1,5,9};
int Order2[4] = {1,2,5,3};
int Order3[3] = {4,4,4};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Expected Output
Order 1 Order 2

This matching method will output
Order2Order1
Could you explain some detail why Order2 will be true?
#include <stdio.h>
int match_arrays(int* arr_order, int* arr_win, int len_order, int len_win)
{
int idx = 0;
int err = 0;
for (int i = 0; i < len_order; i++) {
if (arr_order[i] != arr_win[idx]) {
err++;
if (err > len_order - len_win) {
return 0;
}
} else {
idx++;
if (idx == len_win)
{
return 1;
}
}
}
return 1;
}
int main()
{
int Order1[3] = {1, 5, 9};
int Order2[4] = {1, 2, 5, 3};
int Order3[3] = {4, 4, 4};
int Winning_order[8][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
for (int i = 0; i < 8; i++) {
if (match_arrays(Order1, Winning_order[i], 3, 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 4, 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3, 3)) {
printf("Order3");
}
}
return 0;
}

Related

How to create a cglm Mat4 from a float[16] array?

I know C++ has glm::make_mat4() with #include <glm/gtc/type_ptr.hpp>, but there does not seem to be a make_mat4() function available in CGLM.
Can someone please tell me how to create a CGLM Mat4 from a float[16] array?
I tried the following, but the Mat4 does not seem to print the float values I'm expecting.
#include "cglm/cglm.h"
#include "cglm/call.h"
void main() {
// Atempt #1
float aaa[16] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
mat4 bbb = {
{ aaa[0],aaa[1],aaa[2],aaa[3] },
{ aaa[4],aaa[5],aaa[6],aaa[7] },
{ aaa[8],aaa[9],aaa[10],aaa[11] },
{ aaa[12],aaa[13],aaa[14],aaa[15] }
};
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%d", bbb[i][k]);
printf("\n");
}
}
printf("\n");
// Atempt #2
//void *memcpy(void *dest, const void * src, size_t n)
memcpy(bbb, aaa, sizeof(aaa));
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%d", bbb[i][k]);
printf("\n");
}
}
}
You need to make a void main() function as an entry point. Otherwise the compiler wont know where you program starts.
int main(){
float aaa[16] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16
};
float bbb[4][4] = {
{ aaa[0],aaa[1],aaa[2],aaa[3] },
{ aaa[4],aaa[5],aaa[6],aaa[7] },
{ aaa[8],aaa[9],aaa[10],aaa[11] },
{ aaa[12],aaa[13],aaa[14],aaa[15] }
};
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
printf("%f", bbb[i][k]);
printf("\n");
}
}
printf("\n");
}
This code works in for me.
The diffrence to your code is:
The main function
I changed the data type from the bbb array to float and used the dimensions 4 by 4
I used %f in the printf function for printing the values

How can I create a power set at the end of the program?

i need to create a power set of array lo[], array lo[] is {5, 6, 7}; it's for my laboratories, I don't know how to do this in general power set is 5, {5, 6} {5, 7} {5, 6, 7}, all combinations and another important thing, power of array. the number of power set combinations is the same as the power. Power of array is 2^(elements of array); 2^3 = 8
should be 8 combination.
output:
!((C/A) U (A/B)): {5, 6, 7}
power set: {5}, {5, 6}, {5, 6, 7}, {6, 7}.....
#include <stdio.h>
#include <math.h>
int main() {
int a[10] = { 1,1,1,1,1,1,1,0,0,0 };
int b[10] = { 0,0,0,0,1,1,1,1,1,1 };
int c[10] = { 1,1,1,0,0,0,0,1,1,1 };
int c1[10];
int c2[10];
int c3[10];
int c4[10];
int temp[10], temp1[10], a2[10] = { 1, 2, 3, 4, 5 };
int k = 0;
for (int i = 0; i < 10; i++) {
c1[i] = c[i];
if (c[i] == a[i]) {
c1[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
c2[i] = a[i];
if (a[i] == b[i]) {
c2[i] = 0;
}
}
for (int i = 0; i < 10; i++) {
if (c1[i] == 1 || c2[i] == 1) {
c3[i] = 1;
}
}
for (int i = 0; i < 10; i++) {
if (c3[i] == 0) {
c4[i] = 1;
}
else
c4[i] = 0;
} //// the main part of power set
int lo[3] = {0, 0, 0};
printf("!((C/A) U (A/B)): ");
for (int i = 0; i < 10; i++) {
if (c4[i] == 1) {
printf("%d ", i + 1);
lo[k] = (i + 1);
}
}
}

How to pass a full array in an array of pointers

I wrote a program that sorts the values of an array. It has 5 arrays,
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
and an array of pointers that holds those 5 arrays,
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
I want to sort the values of each array, but when I pass the arrays to the sorting function
sortArrValues(&pArr[i]);
it sees the first index of each array (arr1,arr2...) as the element of that array (pArr[i]), so pArr[i] is (3,2,0,1,4).
But I want pArr[i] to be the full array it meant to be, so in the first iteration pArr[i] will be (3,9,6,7).
Notes: the first index of each array indicates the length of that array (not including that index).
The sorting will skip the first index.
There are two additional functions,they're not used (you can skip them).
Here's the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 5
void sortArrValues(int** arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(&pArr[i]);
}
//sortArrAdress(pArr);
//printArr(pArr);
return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int** arr)
{
int tmp = 0;
int i = 0;
for (i = 1; i < *arr; i++)
{
if (*arr[i] > *arr[i+1])
{
tmp = *arr[i];
*arr[i] = *arr[i + 1];
*arr[i + 1] = tmp;
}
}
}
There is some errors in your code.
First, you don't need to send &pArr[i], pArr[i] is enough to order your arrays. then your void sortArrValues(int** arr) become void sortArrValues(int* arr) which is clearer to read.
Second, in your sortValues, you when you change a value, you should restart (I know that's not very optimized but if you would want to make something really faster, you should use a quick sort. with your code, { 4, 5, 6, 2, 1 } will become { 4 5 2 1 6 }.
So that's your code with the fixes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 5
void sortArrValues(int* arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(pArr[i]);
}
//sortArrAdress(pArr);
//printArr(pArr);
for (int i = 0 ; i < LEN ; i++) {
for (int j = 0 ; j < pArr[i][0] + 1 ; j++)
printf("%d ", pArr[i][j]);
printf("\n");
}
return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int* arr)
{
int tmp = 0;
int i = 0;
for (i = 1; i < *arr; i++)
{
if (arr[i] > arr[i+1])
{
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
i = 0;
}
}
}
I tried to modify your program to fix issues. Some of these issues are already highlighted by others. There were some there were not. Here is the working snippet. Let me know if you need more explanation on any modification. Note that I have only modified your sort function. The idea is not to give you the fastest sorting algorithm, but a working one.
int main()
{
int arr1[] = { 3, 9, 6, 7 };
int arr2[] = { 2, 5, 5 };
int arr3[] = { 0 };
int arr4[] = { 1, 6 };
int arr5[] = { 4, 5, 6, 2, 1 };
int* pArr[] = { arr1, arr2, arr3, arr4, arr5 };
int i = 0;
for (i = 0; i < LEN; i++)
{
sortArrValues(&pArr[i]);
}
}
void sortArrValues(int** arr)
{
int tmp = 0;
int i = 0, j = 0;
for (i = 0; i < (**arr) - 1; i++)
{
for(j=i+1; j < (**arr) - 1; j++) {
if ((*arr)[i+1] > (*arr)[j+1])
{
tmp = (*arr)[i+1];
(*arr)[i+1] = (*arr)[j + 1];
(*arr)[j+1] = tmp;
}
}
printf("%d ", (*arr)[i+1]);
}
printf("%d\n", (*arr)[i+1]);
}

How to add adjacent array items in c with pointers?

#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (int i=0; i<sizeof(num1); i++) {
final[i] = num1[i] + num2[i];
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
So, I did the above without the pointers. But with pointers, here is my attempt: I'm still new to pointers, and I'm playing with different practice problems.
#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (; *num1 != '\0'; *num1++) {
*final = *num1 + *num2;
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
The following does the trick:
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5], c;
int *n1= num1, *n2=num2, *f=final;
for (; n1<&num1[5]; ) {
*f++ = *n1++ + *n2++;
}
for (c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}

num cannot be resolved to a variable in java

I am creating program to sort the total number of hours employees work in a week in descending order. I am only having one compilation error when I try to execute my code. It says that "num" cannot be resolved to a variable...
The error occurs at following line:
sortHours(num); //Error occurs here
Where am I going wrong?
public class WorkerHours {
public static void main(String[] args) {
int[][] hours = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}};
int[] weeklyHours = totalHours(hours);
sortHours(num); //Error occurs here
displayDescSort(weeklyHours);
}
public static int[] totalHours(int[][] hours){
int[] result1 = new int[8];
for (int i = 0; i < hours.length; i++){
int sum = 0;
for (int j = 0; j < hours[i].length; j++){
sum += hours[i][j];
}
result1[i] = sum;
}
return result1;
}
public static void sortHours(int[] num){
for (int i = 0; i < num.length - 1; i++){
int currentMax = num[i];
int currentMaxIndex = i;
for (int j = i + 1; j < num.length; j++){
if (currentMax < num[j]){
currentMax = num[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i){
num[currentMaxIndex] = num[i];
num[i] = currentMax;
}
}
}
public static void displayDescSort(int[] weeklyHours){
for (int i = weeklyHours.length-1; i >= 0; i--){
System.out.println("Employee" + i + ": " + weeklyHours[i] + " hours");
}
}
}

Resources