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.
Related
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 1 year ago.
Improve this question
I want to make a function that multiplies each element in two arrays and store it in the new array.
For example: amount[i] = price[i] * quantity[i];
My code doesn't work. Here is my code:
#include <stdio.h>
void extend(float[], float[], double[]);
int main(void) {
float price[10] = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
float quantity[10] = { 4, 8.5, 6, 8.35, 9, 15.3, 3, 5.4, 2.9, 4.8 };
double amount[10] = { 0 };
extend(price, quantity, amount);
for (int i = 0; i < 10; i++) {
printf("%f ", amount[i]);
}
return 0;
}
void extend(float PRICE[], float QUANTITY[], double amount[]) {
for (int i = 0; i < 10; i++) {
amount[i] = PRICE[i] * QUANTITY[i];
}
}
Please let me know which part is wrong.
code is work on codeblocks. No any error.
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 6 years ago.
Improve this question
I have a problem with counting the sum of values from each line in a multidimensional array in the language C.
Example:
My array with values:
1 2 3 4
5 6 7 8
9 10 11 12
My result array should be:
10
26
42
w - count lines
k - count columns
int tab[w][k]; <-- this is a table just with values(it's example)
int sum[] = {0};
int i,j;
for(i=0;i<w;i++)
{
for(j=0;j<k;j++)
{
sum[i] = sum[i] + tab[i][j];
}
}
It doesn't work well. I've tried do it another way but it only counted the first row.
Please help me, thanks.
sum[] = {0} should be sum[w];, and you should fill it with zeroes before doing the sums.
Then just sum like you did, you can do it better using +=, that works the same as your original code but is easier to write:
#include <stdio.h>
#define w 3
#define k 3
int tab[w][k] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
int main()
{
int sum[w];
int i;
int j;
for (i = 0; i < w; i++)
{
sum[i] = 0;
}
for(i = 0 ; i < w ; i++)
{
for(j = 0 ; j < k ; j++)
{
sum[i] += tab[i][j];
}
printf("sum[%d] = %d\n", i, sum[i]);
}
}
Then your code should run fine
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 6 years ago.
Improve this question
I would like to create an integer list in C containing integer arrays of variable size. The length of the main list will not need to change.
How would I declare it - my implementation - particularly my access is not working:
int dataArray[length];
int dataArray[0] = [1,2,3];
int dataArray[1] = [5,6];
.
.
.
populate
for(int a = 0; a<sizeof(dataArray); a++) {
tempArr = dataArray[a];
for(into = 0; b>sizeof(tempArr); b++) {
print(dataArray[a][b])
}
}
Your main array needs to hold pointers to other arrays. Therefore it should not be int dataArray[length] but rather int* dataArray[length] this means it will hold length amount of references to integer arrays.
int* array[length];
int randomSizeArray[x];
randomSizeArray[0] = 1;
.
.
.
randomSizeArray[x] = 5;
int* array[0] = randomSizeArray;
Also sizeof() will not work the way you expect it to - in C you need to store separately how many elements are in an array. I'd recommend reading a C tutorial from the ground up as you seem to have shaky knowledge of basics.
The sizeof results in the byte count not element count.
Divide the array size by the element size to find the element count.
int dataArray[length];
int dataArray[0] = [1,2,3];
int dataArray[1] = [5,6];
...
// for(int a = 0; a<sizeof(dataArray); a++) {
for(size_t a = 0; a<sizeof(dataArray)/sizeof(dataArray[0]); a++) {
tempArr = dataArray[a];
// for(into = 0; b>sizeof(tempArr); b++) {
for(into = 0; b>sizeof(tempArr)/sizeof(tempArr[0]); b++) {
// or do you really want
// for(size_t = 0; b<sizeof(tempArr)/sizeof(tempArr[0]); b++) {
print(dataArray[a][b])
}
}
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 6 years ago.
Improve this question
For example I have a one dimensional array integers as its elements and I want to make sure that at least one of the element in the array is 5.
How can I achieve this in C language ?
You can write such a function yourself.
Here is a demonstrative program. The function is called any_of like the corresponding algorithm in C++.
#include <stdio.h>
int /* _Bool */ any_of( const int a[], size_t n, int value )
{
size_t i = 0;
while ( i < n && a[i] != value ) i++;
return i != n;
}
int main( void )
{
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
int value = 5;
printf( "The array contains %d is %s\n",
value,
any_of( a, N, value ) != 0 ? "true" : "false" );
return 0;
}
Its output is
The array contains 5 is true
This is a very basic question, however, here's an example, I hope it'll help you in your programming efforts
int main()
{
int a[5] = { 1,2,3,5,3 };
bool fiveFound = false;
for (int i = 0; i < 5; ++i) {
if (5 == a[i]) {
printf("At least one element of the array is 5");
fiveFound = true;
break;
}
}
if (!fiveFound) {
printf("Array doesn't contain 5");
}
}
I'd also suggest you to read The C Programming Language (Second edition) by Brian W. Kernighan and Dennis M. Ritchie, and do some online tutorials
No there is no such shortcut for that in C [And I think not even in any language]. But if you really want this fuctinality you may find it easier to write a function to check whether the element exists in the array or not.
You need to iterate through the elements of the array:
#define SIZE 5
int main() {
int array[SIZE] = {2, 4, 6, 8, 10};
for (int i = 0; i<SIZE; i++){
if (array[i] == 5)
printf("Found it!\n");
return 0;
}
return -1;
}
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 6 years ago.
Improve this question
When printing an array, initializing an integer works.
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i <= (MAX_SIZE - 1); i++)
{
printf("%3d",a[i]);
}
However, I wonder why initializing a pointer to an integer ("walker") won't work:
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; aWalk <= aEnd; aWalk++)
{
printf("%3d", *aWalk);
}
The statement int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; wouldn't even work on its own, so it can't work in a loop header either. The syntax you are looking for is this:
int *ptr1 = some_address, *ptr2 = some_other_address;
This works inside and outside of a loop. Also, note that your problem is not declaring one pointer but two. That's also why you are supposed to first extract a minimal example.
The for initial expression can be a definition for multiple variables as long as it is combined as a single definition:
int a[MAX_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int *aWalk = a, *aEnd = a + MAX_SIZE; aWalk < aEnd; aWalk++) {
printf("%3d ", *aWalk);
}
Note that it is more generic to define the end pointer to point past the end of the array as this form can handle slices of width 0.
You can do it this way only using pointer arithmetic. This is working because you are declaring an array of data and this data is continuously stored in the memory.
int a[] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a; aWalk < (a + sizeof(a) / sizeof(int)); aWalk++)
{
printf("%3d", *aWalk);
}
Also try it here IDE One[^].
edit:
due to the comments I changed the code using an end pointer. code[^].
for (int *aWalk = a, *aEnd = (a + sizeof(a) / sizeof(int)); aWalk < aEnd; aWalk++)
{/*...*/}
You have an extra int in the second code. Also your placement of comma is wrong. Also removed unnecessary aEnd variable.
int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};
for (int *aWalk = a; *aWalk < (a + MAX_SIZE - 1); aWalk++)
{
printf("%3d", *aWalk);
}