Linked List From Flash memory transitioning into Ram - c

Alright so I am new here so please be easy on me :)
I am currently developing on a uC in C and using linked lists to create structures every time the user wants to create one. Example:
typedef struct {
char* dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
}listelement;
listelement * AddItem (listelement * listpointer, char* name, int16_t size, int16_t wordLength, int16_t (*words)[L][Q]) {
// returns listPointer at the beginning of list
listelement * lp = listpointer;
listelement * listPointerTemp;
char ErrorHandler = NULL;
// are we at the end of the list?
if (listpointer != NULL) {
// move down to the end of the list
while (listpointer -> link != NULL)
listpointer = listpointer -> link;
listPointerTemp = listpointer;
listpointer -> link = (struct listelement *) malloc (sizeof (listelement));
// on fail end links becomes NULL already above
if(listpointer -> link != NULL){
listpointer = listpointer -> link;
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc ((size + 1)*sizeof(char));
if(listpointer -> dataitem != NULL){
for(int i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> dataitem[size] = NULL;
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[0][row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return lp;
}
else {
listpointer = (struct listelement *) malloc (sizeof (listelement));
if(listpointer != NULL){
listpointer -> link = NULL;
listpointer -> wordSize = wordLength;
listpointer -> dataitem = (char*) malloc (sizeof(name));
if(listpointer -> dataitem != NULL){
for(int16_t i=0; i<size ; i++){
listpointer -> dataitem[i] = name[i];
}
listpointer -> libWord = (int16_t(*)[Q])malloc(wordLength*Q*sizeof(int16_t));
if(listpointer -> libWord != NULL){
for (int16_t row=0 ; row < wordLength ; row++){
for (int col=0 ; col < Q ; col++){
listpointer -> libWord[row][col] = words[0][row][col];
}
}
ErrorHandler = 1;
}else{
free(listpointer->dataitem);
free(listpointer);
listPointerTemp -> link = NULL;
}
}else{
free(listpointer);
listPointerTemp -> link = NULL;
}
}
if(ErrorHandler == NULL){
//failure
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
usart_write_line(&AVR32_USART0,"Ran out of Memory! Word not created.\r\n");
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
return listpointer;
}
}
So now I want to add structures already stored in memory using const as such:
// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
// TRON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord1[10][Q] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, };
const char dataitem1[4] = {'T','r','o','n'};
const int16_t wordSize1 = 10, nameSize = 4;
//////////////////////////////////////////////////////////////////////////////////////////////////
// eventually change NULL to next link in link list
const listelement Tron = { dataitem1, NULL, wordSize1, libWord1, 0x00 };
Alright so even if you skip reading all of that, I was just wondering what a good way of somehow create constant structs (do I have to create a new structure format just for const types?) and then link the beginning of the RAM linked lists when user creates first one to the Flash portion of linked lists created on startup?
Let me know if more information is needed. I tried not to put too much code on here.
Thanks!
So here is the change and the effects:
// start to include words, then merge into structures for user_interactive to link lists with
//////////////////////////////////////////////////////////////////////////////////////////////////
// TRON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord1[10][Q] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, };
//////////////////////////////////////////////////////////////////////////////////////////////////
// ON
//////////////////////////////////////////////////////////////////////////////////////////////////
const int16_t libWord2[10][Q] = { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
{15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, };
//////////////////////////////////////////////////////////////////////////////////////////////////
// eventually change NULL to next link in link list
const listelement* Tron = { "Tron", NULL, 10, libWord1, 0x00 };
// next element links to Tron . . .
const listelement* On = { "On", Tron, 10, libWord2, 0x01 };
// next element links to On . . . and so on . . .
listelement* constLinkInit(void){
return On;
}
I know I didn't do your global array of structs but ignoring that and passing the last struct to this guy in a nother file using a function.
listelement *listpointer;
// initially listpointer is initialized to NULL but now set to end of link list of FLASH
listpointer = constLinkInit();
And the going to print links like I had been doing for user defined structs dynamically (which had worked) fails when pointing to the next link even though the address passed to listpointer (On) is correct.
void PrintQueue (listelement * listpointer) {
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
if (listpointer == NULL){
usart_write_line(&AVR32_USART0,"queue is empty!\r\n");
}else{
//usart_write_line(&AVR32_USART0," Word List \r\n");
//usart_write_line(&AVR32_USART0,"--------------------------------------------\r\n");
while (listpointer != NULL) {
//usart_write_line(&AVR32_USART0, listpointer -> dataitem);
//usart_write_line(&AVR32_USART0,"\r\n");
//writeCOM_int_array(listpointer -> libWord , listpointer -> wordSize);
listpointer = listpointer -> link; // <---- FAILS HERE
}
}
usart_write_line(&AVR32_USART0,"\r\n--------------------------------------------\r\n");
}
When pointing to the next link it goes to the wrong address . . .
Thanks for the help!

Something like this should work:
#include <stdlib.h>
#include <stdint.h>
#define Q 5
typedef struct {
char *dataitem;
struct listelement *link;
int16_t wordSize;
int16_t (*libWord)[Q];
char gpioValue;
} listelement;
int16_t zzz[Q] = { 11, 22,33,44,55};
listelement global_array[] =
{{ "Sron", global_array+1, 2, zzz , 0 }
,{ "Tron", global_array+2, 2, zzz , 0 }
,{ "Uron", global_array+3, 2, zzz , 0 }
,{ "Vron", NULL , 2, zzz , 0 }
};

Related

How can I swap two bits starting from MSB?

I'm looking for a way to swap 2 bits at a given position but counting starts from MSB(most significant bit) to LSB (least significant bit).
Lets say i have position p1 = 0 and p2 = 2 and my number is 1000 = 8 . The result should be
0010 .
I tried this piece of code but it swaps the bits starting from LSB to MSB . How do I "reverse" the process?
unsigned int bit1 = (num >> p1) & 1;
unsigned int bit2 = (num >> p2) & 1;
unsigned int x = (bit1 ^ bit2);
x = (x << p1) | (x << p2);
result = num ^ x;
By your example, I will assume you mean to start numbering bits from the MSB of the number value. That is, given:
00010100
↑
msb == most significant bit in the number
In that case, you need a way to count the bit index of the MSB. There are actually processor instructions you can use to do that, and both MSVC and GCC (and Clang) provide special functions to access that functionality...
...but you don’t need that. Instead, just write yourself a function:
int index_of_msb( unsigned long value )
{
...
}
Remember that you can shift a number down by one bit at a time. As long as the number is not zero, you have at least one set bit left.
1 0 1 0 0 0
→ 1 0 1 0 0 (1 shift)
→ 1 0 1 0 (2 shifts)
→ 1 0 1 (3 shifts)
→ 1 0 (4 shifts)
→ 1 (5 shifts)
→ 0
Five shifts → bit 5 is MSB.
Now you can convert your bit positions to the standard shift offsets.
p1 = p_msb - p1;
p2 = p_msb - p2;
What remains to do is use your standard bit operators to swap the two bit values.
Four bits is small enough that you can use a 256-byte table:
unsigned char SwapBits(unsigned char number, int p0, int p1)
{
static const unsigned char Table[16][4][4] = {
{ { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 1, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0}, { 0, 0, 0, 0} },
{ { 2, 1, 0, 0}, { 1, 2, 2, 2}, { 0, 2, 2, 2}, { 0, 2, 2, 2} },
{ { 3, 3, 1, 1}, { 3, 3, 2, 2}, { 1, 2, 3, 3}, { 1, 2, 3, 3} },
{ { 4, 2, 1, 0}, { 2, 4, 4, 4}, { 1, 4, 4, 4}, { 0, 4, 4, 4} },
{ { 5, 3, 5, 1}, { 3, 5, 6, 5}, { 5, 6, 5, 4}, { 1, 5, 4, 5} },
{ { 6, 6, 3, 2}, { 6, 6, 5, 4}, { 3, 5, 6, 6}, { 2, 4, 6, 6} },
{ { 7, 7, 7, 3}, { 7, 7, 7, 5}, { 7, 7, 7, 6}, { 3, 5, 6, 7} },
{ { 8, 4, 2, 1}, { 4, 8, 8, 8}, { 2, 8, 8, 8}, { 1, 8, 8, 8} },
{ { 9, 5, 3, 9}, { 5, 9, 9, 12}, { 3, 9, 9, 10}, { 9, 12, 10, 9} },
{ {10, 6, 10, 3}, { 6, 10, 12, 10}, {10, 12, 10, 9}, { 3, 10, 9, 10} },
{ {11, 7, 11, 11}, { 7, 11, 13, 14}, {11, 13, 11, 11}, {11, 14, 11, 11} },
{ {12, 12, 6, 5}, {12, 12, 10, 9}, { 6, 10, 12, 12}, { 5, 9, 12, 12} },
{ {13, 13, 7, 13}, {13, 13, 11, 13}, { 7, 11, 13, 14}, {13, 13, 14, 13} },
{ {14, 14, 14, 7}, {14, 14, 14, 11}, {14, 14, 14, 13}, { 7, 11, 13, 14} },
{ {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15}, {15, 15, 15, 15} },
};
return Table[number][p0][p1];
}

Can't access Array in UseEffect return statement

I am fetching data in React using axios inside a useEffect hook. When I console log the data from inside the useEffect hook everything is fine, but when I attempt to access it in the return statement, it returns Cannot read property 'Con' of undefined.
Here is my code:
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState([]);
useEffect(() => {
axios.get('/api/comRes', {headers:{"Access-Control-Allow-Origin": "http://localhost:3000/"}})
.then(function (data) {
setData(data.data);
console.log(data.data.Con[0])
})}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data.data.Con[0]}</div>
</div>
);
this is the console.log of of the whole datset:
{_id: "5e85c1241c9d440000e730c7", Con: Array(30), Lab: Array(30), LibDem: Array(30), Other: Array(30), …}
_id: "5e85c1241c9d440000e730c7"
Con: (30) [47, 50, 44, 33, 35, 41, 43, 54, 63, 34, 42, 59, 49, 48, 52, 39, 39, 45, 32, 39, 49, 43, 37, 47, 50, 46, 57, 45, 57, 56]
Lab: (30) [31, 28, 34, 47, 42, 33, 35, 23, 19, 44, 34, 21, 28, 29, 34, 36, 39, 31, 11, 29, 33, 45, 52, 41, 30, 38, 28, 29, 25, 22]
LibDem: (30) [9, 9, 10, 9, 11, 11, 8, 8, 9, 10, 9, 9, 14, 9, 4, 8, 8, 10, 6, 5, 10, 8, 5, 7, 11, 7, 9, 14, 11, 14]
Other: (30) [0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]
Brexit: (30) [3, 3, 2, 0, 3, 2, 4, 3, 1, 2, 3, 2, 1, 2, 4, 4, 2, 4, 1, 4, 3, 3, 2, 2, 5, 3, 2, 4, 1, 3]
Green: (30) [4, 4, 4, 6, 3, 5, 5, 5, 2, 4, 5, 3, 4, 4, 3, 4, 4, 5, 3, 1, 4, 2, 3, 3, 4, 5, 4, 8, 4, 5]
SNP: (30) [4, 5, 4, 4, 4, 8, 3, 6, 3, 4, 5, 4, 3, 4, 4, 6, 6, 3, 47, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Plaid: (30) [1, 1, 1, 0, 1, 1, 1, 2, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Date: "20/02/2020"
__proto__: Object
This is the eror report when I try to access {Data.Con[0]} from the return statement:
TypeError: Cannot read property '0' of undefined
First time when your component renders, the data might not be there yet.
My suggestion would be to set the data to null by default, so change this line
const [data, setData] = useState([]);
to
const [data, setData] = useState(null);
Then, before trying to rendering something, check it is there
<div className="dashboard">{data ? data.Con[0] : null }</div>
If you will still have issues, you might want to post the axios's response data structure.
Update #1
import React, { useState, useEffect } from "react";
import axios from 'axios'
import './App.css';
export default function App() {
const [data, setData] = useState();
useEffect(() => {
axios.get('/api/comRes', { headers:
{ "Access-Control-Allow-Origin": "http://localhost:3000/" }
}).then(function (data) {
setData(data.data);
})
}, []);
return (
<div className="grid-container">
<div className="menu"></div>
<div className="dashboard">{data ? data.Con[0] : null}</div>
</div>
);
}
It should just be {data.Con[0]}

How do I put a specific integer into the middle of a sorted array in Swift?

I am writing code where five random numbers are given a value. Those five values are then sorted in an array in numerical order. However, when one value is equaled to 1, I want that number to be in the middle of the sorted array.
When there is no number 1, the sorted array will work correctly.
let fourthRandomNumber:Int = Int(arc4random_uniform(52))
let fifthRandomNumber:Int = Int(arc4random_uniform(52))
let sixthRandomNumber:Int = Int(arc4random_uniform(52))
let seventhRandomNumber:Int = Int(arc4random_uniform(52))
let eighthRandomNumber:Int = Int(arc4random_uniform(52))
var loFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var loEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 1, 1, 1, 1]
var hiFourthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiFifthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSixthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiSeventhRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
var hiEighthRandomNumberOutput: [Int] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11]
let loFourthRandomNumber:Int = loFourthRandomNumberOutput [fourthRandomNumber]
let loFifthRandomNumber:Int = loFifthRandomNumberOutput [fifthRandomNumber]
let loSixthRandomNumber:Int = loSixthRandomNumberOutput [sixthRandomNumber]
let loSeventhRandomNumber:Int = loSeventhRandomNumberOutput [seventhRandomNumber]
let loEighthRandomNumber:Int = loEighthRandomNumberOutput [eighthRandomNumber]
let hiFourthRandomNumber:Int = hiFourthRandomNumberOutput [fourthRandomNumber]
let hiFifthRandomNumber:Int = hiFifthRandomNumberOutput [fifthRandomNumber]
let hiSixthRandomNumber:Int = hiSixthRandomNumberOutput [sixthRandomNumber]
let hiSeventhRandomNumber:Int = hiSeventhRandomNumberOutput [seventhRandomNumber]
let hiEighthRandomNumber:Int = hiEighthRandomNumberOutput [eighthRandomNumber]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var loDealerCardsArray = [loFourthRandomNumberOutput [fourthRandomNumber], loFifthRandomNumberOutput [fifthRandomNumber], loSixthRandomNumberOutput [sixthRandomNumber], loSeventhRandomNumberOutput [seventhRandomNumber], loEighthRandomNumberOutput [eighthRandomNumber]]
//NEED TO WORK OUT THIS ISSUE **(add ace)**
var hiDealerCardsArray = [hiFourthRandomNumberOutput [fourthRandomNumber], hiFifthRandomNumberOutput [fifthRandomNumber], hiSixthRandomNumberOutput [sixthRandomNumber], hiSeventhRandomNumberOutput [seventhRandomNumber], hiEighthRandomNumberOutput [eighthRandomNumber]]
loDealerCardsArray.sort()
loDealerCardsArray[0]
loDealerCardsArray[1]
loDealerCardsArray[2]
print (loDealerCardsArray[0] + loDealerCardsArray[1] + loDealerCardsArray[2])
//NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
hiDealerCardsArray.sort()
hiDealerCardsArray[2]
hiDealerCardsArray[3]
hiDealerCardsArray[4]
print (hiDealerCardsArray[2] + hiDealerCardsArray[3] + hiDealerCardsArray[4])
////NEED TO WORK OUT THIS ISSUE **(PULL IN RANDOM NUMBER FROM ARRAY SO THAT CARD APPEARS IN PROPER LOCATION when there is an ace
var DealerCardsArray = [fourthRandomNumber, fifthRandomNumber, sixthRandomNumber, seventhRandomNumber, eighthRandomNumber]
DealerCardsArray.sort()
DealerCardsArray[0]
DealerCardsArray[1]
DealerCardsArray[2]
DealerCardsArray[3]
DealerCardsArray[4]
For example, five cards have a value of [0, 1, 2, 9, 10], and the array is sorted correctly. I am trying to sort the array so that the 1 is ALWAYS in the middle.
When an ace (value of 1 or 11) appears in the sorted array I want it to look as follows: [0, 2, 1, 9, 10]
Thank you in advance for your assistance
Something like:
var cards = [0, 1, 2, 9, 10]
// Get a list of all the aces in the array.
let aces = cards.filter { $0 == 1 || $0 == 11 }
// Now take the aces out of the array.
cards = cards.filter { $0 != 1 && $0 != 11 }
// Sort the array (unnecessary if its definitely already sorted).
cards = cards.sort()
// Re-insert the aces into the middle.
aces.forEach { cards.insert($0, atIndex: cards.count/2) }
// [0, 2, 1, 9, 10]
Might do the trick (haha)!
var dealerCardsArray = [0, 1, 2, 9, 10]
dealerCardsArray.sort()
// Only if the value 1 exists in the array, remove it and add it back in the center position
if let indexOfOne = dealerCardsArray.index(of: 1) {
dealerCardsArray.remove(at: indexOfOne)
dealerCardsArray.insert(1, at: array.count/2)
}
print(dealerCardsArray) // [0,2,1,9,10]

initialize a struct within a struct initialization?

This may sound somewhat stupid, but I have to know as I'm writing a bingo board in C.
#include <stdio.h>
typedef struct {
int a;
int b;
int c;
int d;
int e;
} row;
typedef struct {
row one;
row two;
row three;
row four;
row five;
} bingo_board;
void initialize_columns()
{
bingo_board board = {
.one = {1, 2, 3, 4, 5},
.two = {6, 7, 8, 9, 10},
.three = {11, 12, 13, 14, 15},
.four = {16, 17, 18, 19, 20},
.five = {21, 22, 23, 24, 25}
};
}
Is this possible?
It can be done simply as
void initialize_columns()
{
bingo_board board = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
}
Or even as
void initialize_columns()
{
bingo_board board = {
1, 2, 3, 4, 5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25
};
}
No need to "tag" every row. The tagged syntax is available in C99 though, and what you have in your example is already correct for C99.
Because structs are first class citizens in c, assignment is well defined, this lets you
static bingo_board initial_board = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
void init_board(bingo_board *b)
{
*b = initial_board;
}
Which seems to be what you want. If you do declare the board within the function, I would suggest declaring it static, because you don't modify it, so persistent changes are ok, and so that the function doesn't have to grow the stack as much on every call.

create this array

i know this sounds silly, but can someone please post the arrays described by rfc2612:
Cm = 0x5A827999
Mm = 0x6ED9EBA1
Cr = 19
Mr = 17
for (i=0; i<24; i++)
{
for (j=0; j<8; j++)
{
Tmj_(i) = Cm
Cm = (Cm + Mm) mod 2**32
Trj_(i) = Cr
Cr = (Cr + Mr) mod 32
}
}
i think im doing is wrong for some reason
i get this for Tr
[[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2],
[10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2]]
Tr0 = { 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11 }
Tr1 = { 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28 }
Tr2 = { 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13 }
Tr3 = { 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30 }
Tr4 = { 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15 }
Tr5 = { 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0 }
Tr6 = { 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17 }
Tr7 = { 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2 }
..and Tm (in hex):
Tm0 = { 5a827999, d151d6a1, 482133a9, bef090b1, 35bfedb9, ac8f4ac1, 235ea7c9, 9a2e04d1, 10fd61d9, 87ccbee1, fe9c1be9, 756b78f1, ec3ad5f9, 630a3301, d9d99009, 50a8ed11, c7784a19, 3e47a721, b5170429, 2be66131, a2b5be39, 19851b41, 90547849, 723d551 }
Tm1 = { c95c653a, 402bc242, b6fb1f4a, 2dca7c52, a499d95a, 1b693662, 9238936a, 907f072, 7fd74d7a, f6a6aa82, 6d76078a, e4456492, 5b14c19a, d1e41ea2, 48b37baa, bf82d8b2, 365235ba, ad2192c2, 23f0efca, 9ac04cd2, 118fa9da, 885f06e2, ff2e63ea, 75fdc0f2 }
Tm2 = { 383650db, af05ade3, 25d50aeb, 9ca467f3, 1373c4fb, 8a432203, 1127f0b, 77e1dc13, eeb1391b, 65809623, dc4ff32b, 531f5033, c9eead3b, 40be0a43, b78d674b, 2e5cc453, a52c215b, 1bfb7e63, 92cadb6b, 99a3873, 8069957b, f738f283, 6e084f8b, e4d7ac93 }
Tm3 = { a7103c7c, 1ddf9984, 94aef68c, b7e5394, 824db09c, f91d0da4, 6fec6aac, e6bbc7b4, 5d8b24bc, d45a81c4, 4b29decc, c1f93bd4, 38c898dc, af97f5e4, 266752ec, 9d36aff4, 14060cfc, 8ad56a04, 1a4c70c, 78742414, ef43811c, 6612de24, dce23b2c, 53b19834 }
Tm4 = { 15ea281d, 8cb98525, 388e22d, 7a583f35, f1279c3d, 67f6f945, dec6564d, 5595b355, cc65105d, 43346d65, ba03ca6d, 30d32775, a7a2847d, 1e71e185, 95413e8d, c109b95, 82dff89d, f9af55a5, 707eb2ad, e74e0fb5, 5e1d6cbd, d4ecc9c5, 4bbc26cd, c28b83d5 }
Tm5 = { 84c413be, fb9370c6, 7262cdce, e9322ad6, 600187de, d6d0e4e6, 4da041ee, c46f9ef6, 3b3efbfe, b20e5906, 28ddb60e, 9fad1316, 167c701e, 8d4bcd26, 41b2a2e, 7aea8736, f1b9e43e, 68894146, df589e4e, 5627fb56, ccf7585e, 43c6b566, ba96126e, 31656f76 }
Tm6 = { f39dff5f, 6a6d5c67, e13cb96f, 580c1677, cedb737f, 45aad087, bc7a2d8f, 33498a97, aa18e79f, 20e844a7, 97b7a1af, e86feb7, 85565bbf, fc25b8c7, 72f515cf, e9c472d7, 6093cfdf, d7632ce7, 4e3289ef, c501e6f7, 3bd143ff, b2a0a107, 296ffe0f, a03f5b17 }
Tm7 = { 6277eb00, d9474808, 5016a510, c6e60218, 3db55f20, b484bc28, 2b541930, a2237638, 18f2d340, 8fc23048, 6918d50, 7d60ea58, f4304760, 6affa468, e1cf0170, 589e5e78, cf6dbb80, 463d1888, bd0c7590, 33dbd298, aaab2fa0, 217a8ca8, 9849e9b0, f1946b8 }
(I'm not sure why they didn't just include these as tables in the spec).

Resources