Create Integer List of Lists in C [closed] - c

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])
}
}

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/C++ : Why is it impossible to declare a pointer in loop initialization [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 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);
}

How to make a Worst case in mergesort in c? [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
DataCount is how many times at sorting numbers.
int* MakeMWData(int DataCount)
{
// make array
int* Data = (int*)malloc(DataCount*sizeof(int));
int number = 2;
int count = 0;
Data[0] = 1;
// input data
int i,j;
for( i = DataCount;; i/=2)
{
count++;
for( j = 1; j<DataCount;j++)
{
//merge sort worst
i think this isn't correct.
if(j%i == 0 && j %(i * 2) != 0)
{
Data[j] = number;
number++;
}
}
if(i==1)
break;
}
for( i = 0; i<DataCount ; i++)
{
if(Data[i] ==0)
Data[i] = number;
number++;
}
return Data;
}
Making worst Data in main function.
int* MergeData = MakeMWData(DataCount[i]);
The way mergesort works is dividing the array in two arrays, recursively (logn times), untill being able to compare pairs of elements. Then it merges the recursively created arrays also sorting them at the same time.
For some sorting algorithms (e.g. quicksort), the initial order of the elements can affect the number of operations to be done. However it doesn't make any change for mergesort as it will have to do exactly the same number of operations anyway: recursively divide into small arrays and then merge them back, in total Θ(nlogn) time.

my C code is too awkward [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Assume the character a,b,c,d,e represent the number 1 to 9, and they cannot be equal to
each other.
Question:
How many equals that can meet (ab * cde = adb * ce).
Example:
36 * 495 = 396 * 45.
Here is my code,and the result is right.However,i think my code is too awkward,especially in (if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0))
I would appreciate it if someone could give me a better solution.
#include<stdio.h>
main(){
int a,b,c,d,e,m,n,i=0;
long f1,f2,f3,f4;
for(m=11;m<=99;m++){
a=m/10;
b=m%10;
if(a!=b&&a*b!=0)
{
for(n=101;n<=999;n++)
{
c=n/100;
d=n%100/10;
e=n%10;
if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e&&c*d*e!=0)
{
f1=a*10+b;
f2=c*100+d*10+e;
f3=a*100+d*10+b;
f4=c*10+e;
if(f1*f2==f3*f4) i++;
printf("\n%d%d*%d%d%d*=%d%d%d*%d%d\n",a,b,c,d,e,a,d,b,c,e);
}
}
}
}
printf("%d\n",i);
return 0;
}
If you can, instead of
int a,b,c,d,e;
Try to use
int numbers[5];
And then to check if your numbers are all different, you can use for loops
doubleOccurence = FALSE; /* where FALSE = 0 */
for (i=0; i < 4; i++) {
for (j=i+1; j < 5; j++) {
doubleOccurence = doubleOccurence || (numbers[i] == numbers[j]);
}
}
It looks a bit clearer to me.
Unfortunately you can't really iterate through a list of variables you are better off with an array of numbers like Julien mentions in his answer.
int nums[5];
replace a with nums[0], b with nums[1], etc....
But then I would go one step further to tidying up your code and call a function that takes in the array to check uniqueness:
if(listIsUnique(nums, 5)) // yes hardcoded the 5, but that can be sorted
{
...
}
And then:
bool listIsUnique(int* nums, int len)
{
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
if (nums[i] == nums[j])
return false; // return false as soon as you find a match - slightly faster :)
return true; // if we get here its a unique list :)
}
Note: code is untested, there may be mistakes :o

Resources