Three dimensional array function argument c [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am working on a lab and I need some help!
Lets say I have a 3 dimensional array double a[2][3][4]. I want to update the values of inside this array through void functions. How would I set that up? I am having trouble initializing a pointer to the double that I want send in as an argument. I want to do something to every value inside of the 3d array so all 24 values?

You probably want this:
void MyFunction (double a[2][3][4])
{
int i,j,k ;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
for (k = 0; k < 4; k++)
{
a[i][j][k] = 5.0 ;
}
}
void main()
{
double a[2][3][4] ;
MyFunction(a) ;
// now every element of array a contains 5.0
}

Related

Copying data to an integer pointer array through loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am facing issue copying data to void pointer array through for loop. I am not allowed to use double pointer.
I am trying someting like this:
for(i=0;i<9;i++){
array + i = i;
}
but I cant just do this, and I know that,
I am trying to do it like this
memcpy(
array+i,
i,
s
);
I have already allocated memory:
My actual code is some thing like this:
if(v->e_type==V_INT){
// printf("%p ",v->e_array+v->no_e);
memcpy(
v->e_array+v->no_e,
new_val,
v->e_sz
);
}
If you have something like this:
int *x = malloc(sizeof(int)*4);
You can put values in it with pointer arithmetic:
for(int i = 0; i < 4; i++) {
*(x+i) = i; // or *(x+1) = 0; for example
}
Or simply:
for(int i = 0; i < 4; i++) {
x[i] = i;
}

How to correctly pass an array to struct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Question Edited
I'm very new in C, and here.
Is it possible for an array return its value via a struct?
Although I'm trying to process each character in List[5] = {1, 2, 3, 4}, however, it only sticks at 1 and only prints 575757 rather than
My struct
struct Count numbers() {
struct Count numbers;
int List[5] = {1, 2, 3, 4};
int i = 0;
for (i = 0; i < 10; i++) { //It might be something in my for loop
numbers.intOne= List[i] + 4; // 1 + 4
numbers.intTwo= List[i] + 6; // 1 + 6
return numbers;
}
};
This only prints 575757, i wish this to print 576879
void printCode(struct Count numbers) {
int i;
for (i = 0; i < 3; i++) {
printf("%i%i", numbers.intOne, numbers.intTwo);
}
}
The main
int main() {
int i = 0;
for (i = 0; i < 10; i++) {
numbers();
printCode(numbers());
getchar();
}
}
Thank you for any help!
it only stucks at '4352'
You are returning from decode soon after processing the first element of the output array which is 4352. So the other elements of check and param arrays do not get filled up.
In any case, check and param are arrays local to the decode function and the values of their elements cannot be used outside the function.

C code for finding 1(factorial)2(fac)+2(fac)3(fac)+...9(fac)10(fac) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to programming and have just started with array. This was my code for the problem
#include <stdio.h>
int main(){
int i=1,f=1,num,fac[10],sum=0;
for (num=1; num<=10; num++) {
for (i; i<=num; i++) {
f = f * i;
}
fac[num-1]=f;
}
for(i;i<=9;i++)
sum = sum + fac[i] * fac[i+1];
printf("The sum is %d",sum );
return 0;
}
Output it is giving is-The sum is 0
So what are the corrections to be made or any other code for the problem?
You need to reinitialize the variable i more often.
Instead of having a for loop that looks like for (i; i<=N; i++), initialize i and do for (i=0; i<=N; i++)

How to print char value in int array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array board[3][3] of type int. I want to show 8 numbers and on the last place "_" must be shown. I don't know how to do that. Please help
If the 8 numbers you want print is the first 8, you can just use a loop to with printf to print it until the last number:
int j, k;
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
if (j == 2 && k == 2)
printf("_");
else
printf("%d ", board[j][k]);
}
printf("\n");
}

Why does a structure type with an int field "forget" what value it is after the first time around a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why does the following code only work on the first iteration around the for loop?
typedef struct {
char name[3];
int gold, silver, bronze, total;
} tally_t;
int main(void)
{
tally_t country[COUNTRIES_COMPETING];
int j;
j=0;
country[j].gold=0;
for (j=0; j<5; j++) {
country[j].gold++;
}
return 0;
}
It's because you only initialized the gold member of the first element of the array. All the rest are uninitialized and have undefined values. Changing an undefined value is undefined behavior.
You just initialized the first element of your array of structure.
In order to fix your problem, you can do it like this :
int main(void)
{
tally_t country[COUNTRIES_COMPETING];
int j;
// Init Step
for (j = 0; j < 5; j++)
country[j].gold = 0:
for (j=0; j<5; j++)
country[j].gold++;
return 0;
}

Resources