Filling values in 1D array - c

Code Fragment
if (val==1)
paperR[LIMIT]={100,50,20,10,5,2,1};
else if (val==2)
paperR[LIMIT]={200,100,50,20,10,5,1};
PROBLEM ?
it's like doing
int ask;
LATER IN CODE
ask=1;
How to do it with array ?

You can't assign to an array, you can only provide a list of values if you're initializing the array at the point where it's being declared.
If you want to fill in an array, you can use memcpy from another array that contains the values you want to use. So you can declare:
const int arr100[] = {100,50,20,10,5,2,1};
const int arr200[] = {200,100,50,20,10,5,1};
int paperR[LIMIT];
if (val == 1) {
memcpy(paperR, arr100, sizeof arr100);
} else if (val == 2) {
memcpy(paperR, arr200, sizeof arr200);
}

You may do it by one element after other:
if (val==1)
{
paperR[0] = 100;
paperR[1] = 50;
paperR[2] = 20;
paperR[3] = 10;
paperR[4] = 5;
paperR[5] = 2;
paperR[6] = 1;
}
else if (val==2)
{
paperR[0] = 200;
paperR[1] = 100;
paperR[2] = 50;
paperR[3] = 20;
paperR[4] = 10;
paperR[5] = 5;
paperR[6] = 1;
}

Related

why isn't this code storing the words in this 2d array?

The code is suppose to take in 6 words starting with a, b or c and put them in to each respective array. Only problem is that it doesn't store anything at all no matter what I do.
The printf in the last part of the code is suppose to spit everything that I typed in out but it's giving me nothing but blanks...
#include <stdio.h>
int main()
{
char startswithA[6][10] = {};
char startswithB[6][10] = {};
char startswithC[6][10] = {};
char holder[6][10] = {};
printf("Enter a word starting with a, b or c (write 2 of each)\n");
for (int i = 0; i < 6; i = i + 1)
{
printf("Now entering word #%d\n", i+1);
scanf_s("%s", &holder[i]);
if(holder[i][0] == 'a')
{
for(int a = 0; a < 10; a = a + 1)
{
holder[i][a] = startswithA[i][a];
}
}
else if (holder[i][0] == 'b')
{
for(int a = 0; a < 10; a = a + 1)
{
holder[i][a] = startswithB[i][a];
}
}
else if (holder[i][0] == 'c')
{
for(int a = 0; a < 10; a = a + 1)
{
holder[i][a] = startswithC[i][a];
}
}
else
{
printf("something out of bound was typed in");
}
}
for(int b = 0; b < 6; b = b + 1)
{
printf("%s %s %s\n", startswithA[b], startswithB[b], startswithC[b]);
}
return 0;
}
Looks like it is typo
holder[i][a] = startswithA[i][a];
Did you mean?
startswithA[i][a] = holder[i][a];

how to declare an array based on if else condition inside a function and calling this array

Hi I am declaring an array based on condition in a function when I am calling this function I am getting below error
main.c:24:17: error: ‘rsi’ undeclared (first use in this function)
rsii[i]=rsi[i];
This is my code:
#include<stdio.h>
#include <stdint.h>
void RootSequenceIndex( int RSIIndex, uint32_t rsii[4])
{
//uint32_t rsi[839];
if (RSIIndex==0)
{
uint32_t rsi[4] = {1,2,3,4};
}
else if(RSIIndex==1)
{
uint32_t rsi[4] = {5,6,7,8};
}
else if(RSIIndex==2)
{
uint32_t rsi[4] = {9,10,11,12};
}
else
{
uint32_t rsi[4] = {13,14,15,16};
}
for (int i=0;i<4;i++)
{
rsii[i]=rsi[i];
}
}
int main()
{
int RSIIndex=0;
uint32_t rssi[4] ;
RootSequenceIndex( RSIIndex,rssi);
for ( int i = 0; i < 4; i++ ) {
printf( "%d\n", rssi[i]);
}
//return 0;
}
Your array definitions are only visible inside the block where they are defined. You seem to try this because you can only provide initialization values when you define a variable.
To avoid that problem, you could try this:
#define RSI_ELEMENTS 4
void RootSequenceIndex( int RSIIndex, uint32_t rsii[RSI_ELEMENTS])
{
static const uint32_t rsi[][RSI_ELEMENTS] = {{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
size_t MaxIndex = sizeof(rsi)/sizeof(rsi[0]);
if (RSIIndex > MaxIndex - 1 || RSIIndex < 0)
RSIIndex = MaxIndex - 1 ;
size_t MaxElement = sizeof(rsi[0])/sizeof(rsi[0][0]);
for (int i=0; i < MaxElement; i++)
{
rsii[i]=rsi[RSIIndex][i];
}
}
As already written in other answers you have to declare rsi at function scope instead of in if blocks.
To avoid assigning array elements in a loop you can use a pointer rsi instead of an array and assign a constant array to it.
void RootSequenceIndex( int RSIIndex, uint32_t rsii[4])
{
const uint32_t *rsi;
if (RSIIndex==0)
{
rsi = (const uint32_t[4]){1,2,3,4};
}
else if(RSIIndex==1)
{
rsi = (const uint32_t[4]){5,6,7,8};
}
else if(RSIIndex==2)
{
rsi = (const uint32_t[4]){9,10,11,12};
}
else
{
rsi = (const uint32_t[4]){13,14,15,16};
}
for (int i=0;i<4;i++)
{
rsii[i]=rsi[i];
}
}
Note: I consider the other solution with a two-dimensional array better than this one.
The array you declare only exists within the scope it is declared, i.e. within each if block, so it is not accessible outside of that.
You would need to declare the array outside the if block, then assign values to it inside:
void RootSequenceIndex( int RSIIndex, uint32_t rsii[4])
{
uint32_t rsi[4];
if (RSIIndex==0)
{
rsi[0] = 1;
rsi[1] = 2;
rsi[2] = 3;
rsi[3] = 4;
}
else if(RSIIndex==1)
{
rsi[0] = 5;
rsi[1] = 6;
rsi[2] = 7;
rsi[3] = 8;
}
else if(RSIIndex==2)
{
rsi[0] = 9;
rsi[1] = 10;
rsi[2] = 11;
rsi[3] = 12;
}
else
{
rsi[0] = 13;
rsi[1] = 14;
rsi[2] = 15;
rsi[3] = 16;
}
for (int i=0;i<4;i++)
{
rsii[i]=rsi[i];
}
}
Alternately, you can get rid of the array altogether and use another loop to generate the values that would be in it.
void RootSequenceIndex( int RSIIndex, uint32_t rsii[4])
{
for (int i=0;i<4;i++)
{
rsii[i] = RSIIndex * 4 + 1 + i;
}
}

Modify variable or struct inside a C function

I wanted to make a game of life in C to learn C, but it doesn't work. I just noticed that my code doesn't work when I try to modify a variable created inside a function, even if I try to modify it inside the same function.
When I pass val = 1 in comment, and color in comment, the code works
so val = 0 doesn't cause problem, neither color = {0,0,0,0} because it doesn't modify the variable.
How can I do to correct it ?
void next_grid(int nbc, int nbr,Cell tab[][120],Cell newtab[][120]){
for (int y = 0; y<120; y++){
for(int x = 0; x<120; x++){
int nb_neighbors = count_neighbors(tab,120,120,y,x);
int val = 0;
Color color = {0,0,0,0};
// When a cell is dead
if (tab[y][x].val == 0)
{
if (nb_neighbors == 3)
{
val = 1;
color = (Color) {230,160,0,255};
}
else{
val = 0;
color = (Color) {0,0,0,255};
}
}
{
if (nb_neighbors == 2 || nb_neighbors == 3)
{
val = 1;
color = (Color) {230,160,0,255};
}
else
{
val = 0;
color = (Color) {0,0,0,255};
}
}
newtab[y][x] = (Cell) {val,x,y,color};
}
}
// swap array
for (int y = 0; y<120; y++){
for(int x = 0; x<120; x++){
tab[y][x] = (Cell) newtab[y][x];
}
}
}

C Program numbers not sorting array correctly

I have an array of structs for products that I am trying to sort by name, type, price, and quantity. Name and type work, but price and quantity aren't working.
My code is:
else if (sort == sortByPrice)
{
for (int i = 0; i < numProducts; i++)
{
int smallPosition = i;
for (int x = i + 1; x < numProducts; x++)
{
if (list[i].price > list[x].price)
{
smallPosition = x;
}
}
temp = list[i];
list[i] = list[smallPosition];
list[smallPosition] = temp;
}
}
else if (sort == sortByQty)
{
for (int i = 0; i < numProducts; i++)
{
int smallPosition = i;
for (int x = i + 1; x < numProducts; x++)
{
if (list[i].qty > list[x].qty)
{
smallPosition = x;
}
}
temp = list[i];
list[i] = list[smallPosition];
list[smallPosition] = temp;
}
}
Can anyone tell me why it doesn't work/how to fix it?
Following up on Lee Daniel Crocker's comment, you should dynamically compare with the value at smallPosition instead of i so that it will always point to the smallest remaining item:
int smallPosition = i;
for (int x = i + 1; x < numProducts; x++)
{
if (list[smallPosition].price > list[x].price)
{
smallPosition = x;
}
}
You should move the swap code inside the if statement:
for (int i = 0; i < numProducts; i++)
{
for (int x = i + 1; x < numProducts; x++)
{
if (list[i].price > list[x].price)
{
temp = list[i];
list[i] = list[X];
list[X] = temp;
}
}
}
Just use bubble sort.
In the bubble sort you swap the values of the current value is bigger than the next one, if you do this action until you get to the end of the array then the array will be sorted.

code with double free or corruption

I've got a double free problem in my program. I know which pointer is double freed but I cant figure out when was it freed first.
here is the code of my function :
int spectrum_gen(char *shift_r, char *rec_poly, char *redun_poly,int spectrum_length)
{
char *seq = NULL,*l_shift = NULL,loop_shift[SIZE];
int current_weight,*spectrum = NULL,*spect_numb = NULL,length=1,spectrum_size=0;
int index,index2,temp,temp2,*temp3;
/* int *weights = NULL; */
int *encoded_w = NULL;
int min_length,min_weight = 1000;
int looping = 0;
int **spectrum_content = NULL;
int *seq_w;
int *weight_table = symbols_weight(Q);
int *weights = NULL;
spectrum= (int*) malloc(sizeof(int));
seq = (char*) malloc(sizeof(char));
l_shift = (char*) malloc(SIZE*sizeof(char*));
weights = (int*) malloc(sizeof(int));
encoded_w = (int*) malloc(sizeof(int));
spect_numb = (int*) malloc(sizeof(int));
spectrum_content = (int**) malloc(sizeof(int*));
spectrum_content[1] = (int*) malloc(sizeof(int));
seq_w = (int*) malloc(sizeof(int));
strcpy(seq,"1");
convert_to_real(seq,1);
while(length > 0)
{
/* show_word(seq,length);
show_word(shift_r,SIZE);
puts(""); */
if(length == 1)
{
set2zero(shift_r,SIZE);
current_weight = RTZ_weight(0,seq[0],shift_r,rec_poly,redun_poly,encoded_w,seq_w,weight_table);
*seq_w += seq_weight(seq,1,weight_table);
}
else
{
current_weight = RTZ_weight(weights[length-2],seq[length-1],shift_r,rec_poly,redun_poly,encoded_w,seq_w,weight_table);
*seq_w += seq_weight(seq,length,weight_table);
/* show_word(shift_r,SIZE);
show_word(loop_shift,SIZE); */
if(looping==1 && str_cmp(shift_r,loop_shift,SIZE))
{
puts("looping sequence!!");
free(spectrum);
spectrum = NULL;
free(encoded_w);
encoded_w = NULL;
free(spect_numb);
spect_numb = NULL;
free(spectrum_content);
spectrum_content = NULL;
free(weights);
weights = NULL;
return 1;
break;
}
if(*encoded_w==weights[length-2] && looping==0 && length>3)
{
str_cpy(loop_shift,shift_r,SIZE);
looping = 1;
}
if(*encoded_w != weights[length-2])
{
looping = 0;
}
}
weights = realloc(weights,length*sizeof(int));
weights[length-1] = *encoded_w;
l_shift = realloc(l_shift,length*sizeof(char));
l_shift[length-1] = shift_r[SIZE-1];
if((shift_r[0] != 0) && (*encoded_w < spectrum_length))
{
if((temp = index4(current_weight,spectrum,spectrum_size,1,0)) != (-1))
{
spect_numb[temp]++;
if((temp2 = index4(*seq_w,spectrum_content[temp],spectrum_content[temp][0],2,1)) != (-1))
{ spectrum_content[temp][temp2+1]++;
}
else
{
spectrum_content[temp][0] += 2;
spectrum_content[temp] = realloc(spectrum_content[temp],spectrum_content[temp][0]*sizeof(int));
spectrum_content[temp][spectrum_content[temp][0]-2] = *seq_w;
spectrum_content[temp][spectrum_content[temp][0]-1] = 1;
}
}
else
{
spectrum_size++;
spectrum = realloc(spectrum,spectrum_size*sizeof(int));
spect_numb = realloc(spect_numb,spectrum_size*sizeof(int));
/* spectrum content : creation of a new table to store the inputs with output of weight current_weight*/
spectrum_content = realloc(spectrum_content,spectrum_size*sizeof(int*));
spectrum_content[spectrum_size-1] = (int*) malloc(3*sizeof(int));
spectrum_content[spectrum_size-1][0] = 3;
spectrum_content[spectrum_size-1][1] = *seq_w;
spectrum_content[spectrum_size-1][2] = 1;
spectrum[spectrum_size-1] = current_weight;
spect_numb[spectrum_size-1] = 1;
}
}
if(seq_equal_zero(shift_r,SIZE) || (*encoded_w >= spectrum_length))
{
while((length>0) && (seq[length-1] == Q-1))
{
length--;
for(index=0;index<SIZE-1;index++)
shift_r[index] = shift_r[index+1];
shift_r[SIZE-1] = l_shift[length-1];
}
for(index=0;index<SIZE-1;index++)
shift_r[index] = shift_r[index+1];
shift_r[SIZE-1] = l_shift[length-2];
seq[length-1] += 1;
}
else
{
length++;
seq = realloc(seq,length*sizeof(char));
seq[length-1] = 0;
}
/* printf("%d\n%d\n",*encoded_w,current_weight);
getchar(); */
}
/* sort(spectrum,spect_numb,spectrum_content,spectrum_size);*/
puts("Debut du spectre de ce codeur :");
for(index=0;spectrum[index+1]<=spectrum_length;index++)
{
printf("( ");
for(index2=1;index2<spectrum_content[index][0]-2;index2+=2)
{
printf("%d*I^%d + ",spectrum_content[index][index2+1],spectrum_content[index][index2]);
}
printf("%d*I^%d",spectrum_content[index][spectrum_content[index][0]-1],spectrum_content[index][spectrum_content[index][0]-2]);
printf(" )*O^%d + ",spectrum[index]);
}
printf("( ");
for(index2=1;index2<spectrum_content[index][0]-2;index2+=2)
{
printf("%d*I^%d + ",spectrum_content[index][index2+1],spectrum_content[index][index2]);
}
printf("%d*I^%d",spectrum_content[index][spectrum_content[index][0]-1],spectrum_content[index][spectrum_content[index][0]-2]);
printf(")*O^%d",spectrum[index]);
puts("");
free(seq);
seq = NULL;
free(spectrum);
spectrum = NULL;
free(encoded_w);
encoded_w = NULL;
free(spect_numb);
spect_numb = NULL;
free(spectrum_content);
spectrum_content = NULL;
free(weights);
weights = NULL;
return 0;
}
that pointer is called seq.
It would be so cool if someone helps me with this :)
here are the two functions that handle that pointer
void convert_to_real(char *word,int end)
{
int index;
for(index=0;index<end;index++) word[index]-='0';
}
i dont think it may be a problem
the only other function that handles that pointer is :
int seq_weight(char *seq,int end,int *weight_table)
{
int index,weight = 0;
for(index=0;index<end;index++)
if(seq[index]>=0 && seq[index]<Q)
weight += weight_table[(int)seq[index]];
return weight;
}
and i dont think it would cause a problem neither. :(
It's great that you set the pointer value to null after you have free'd it. So make use of that fact, and check for null before you free it - that way you avoid the double delete. Then you don't have to hunt for the double deletion because you will be protected from it.

Resources