Group specific array elements together C - c

Say, you have an array,
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
Is it possible to output the display as such:
[0 6 12, 1 7 13, 2 8 14, 3 9 15, 4 10 16]
Such that each new array element consists of 3 previous array elements in that pattern, where every 6th element is not included.
so far all I can think of is, creating a temporary array, looping over the array elements and copying the respected element across, but I have no idea how to approach that. Sorry I'm having difficulty providing example code, as the whole concept confuses me.
int i,j,adder = 0;
for(i=0;i<5;i++){ //loop over 5 times for new array of size 5
adder += 1;
for(j=i+(i*5);j<i+(5*adder);j++){ //changing between elements and not including 6th value
//stuck here
}
printf("%d", arr[i]);
}

You had the right idea to use nested loops, but the inner loop needs to increment by 6 every time.
printf("[");
for (i = 0; i < 5; i++) {
printf("[");
for (j = i; j < 17; j += 6) {
printf("%d ", arr[j]);
}
printf("]%s", (i < 4 ? ", " : ""));
}
printf("]");

Related

Finding the number of elements in an modified array (duplicates removed)

My array size (in terms of actual number of elements in it visually) will change so I have established an "open-ended" array with a relatively large size that I know it will never exceed.
I would add elements to an array using for loop
for(i=0; i<=100; i++) {
array[i] = i;
}
and I would remove duplicates from the array using
#define length 100
for(i=0; i<length; i++)
{
for(j=i+1; j<length; j++)
{
if(array[i] == array[j])
{
for(k=j; k < length - 1; k++)
{
array[k] = array[k + 1];
}
length--;
j--;
}
}
}
and find the array size using
int length = sizeof(setarray)/sizeof(setarray[0]);
It seems that after the modification
e.g. : {1 2 4 4 3 5 6 2 1 0} to {1 2 4 3 5 6 0}
the array size seems to stay the same. I was wondering if there is a method to find the array size after modification (i.e: add, remove elements).

Copy an array to another while sorting it simultaneously

I have already done the sorting part and the copy but I am really struggling to put it together.
Here is only the sorting part because i am not so familiar with it. My main problem is when I start copying the array it only copies the first 2 number then stops and I think its a small problem in the for loop somewhere but i cant find it.
int main ()
{
int number[30];
int number2[30];
int i, j, a, n;
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The array is copied and sorted like:\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", number[i]);
}
system("Pause");
}
I compiled (MinGW) and ran this exact code and got the following output:
Enter the value of N
10
Enter the numbers
9
1
0
2
8
3
7
5
4
6
The array is copied and sorted like:
9
8
7
6
5
4
3
2
1
0
Press any key to continue . . .
It looks like it is correctly reverse-sorting the input values. Note that this is a fairly common sorting algorithm (https://www.geeksforgeeks.org/bubble-sort/).
Based on this I would argue that the for loop works as is, assuming that you are looking for a reverse sort.
I am not sure what exactly is meant by "copying the array". Array number2 is unused and there are no operations that appear to copy the array number, just operations for loading it, sorting it and printing it.

How do I fix the problem with the incorrect array value in for loop using C language? [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 2 years ago.
I am trying to understand how array boundaries work in C, so tried the following code:
#include <stdio.h>
int main(){
{
int i, s[4], t[4], u=0;
// fisrt loop
for (i=0; i<=4; i++)
{
s[i] =i;
t[i] =i;
printf("s = %d\n", s[i]);
printf("t = %d\n", t[i]);
}
// second loop
printf("s:t\n");
for (i=0; i<=4; i++)
printf("%d:%d\n", s[i], t[i]);
printf("u = %d\n", u);
}
return 0;
}
The output is as follows :
fisrt loop
s = 0
t = 0
s = 1
t = 1
s = 2
t = 2
s = 3
t = 3
s = 4
t = 4
second loop
s:t
4:0
1:1
2:2
3:3
4:4
u = 0
I am expecting both loops to print 5 elements from 0 to 4. As you can see, the first for loop's output looks ok, but in the second loop the value of s[0]
is wrong and for some reason turned to 4.
I am assuming that this is happening because of some problem in the array boundary, but I am not sure, please correct me if I am wrong. Is there any way to fix this, and make s[0] correct value? Cheers.
for (i=0; i <4; i++)
in your code you access 5 elements of the array and your array has only 4 elements. It ans Undefined Behaviour.
Second loop the same error.
There is a problem with the size of the array, you need to modify your code of that line to given code:
int i, s[4], t[4], u=0;
to
int i, s[5], t[5], u=0;
Delete the equals in the for loops:
for (i = 0; i < 4; i++)
The length of the arrays is 4. From 0 to 4 (included) there are 5 positions.
When you declare int s[4]; you create an arrays of lenght 4: s[0], s[1], s[2], s[3]. You can't access s[4].
This is because the position starts to 0.
If you want to have 5 elements for both arrays:
Change the declarations of s and t to s[5] and t[5]. Only at the declaration the 5 means 5 elements. Only at indexing and addressing a certain element, counting starts at 0, not 1.
If you want to have the arrays to be consisted of 4 elements:
for (i = 0; i <= 4; i++)
i <= 4 - With this condition and the initialization i = 0 you access memory beyond the bounds of the array at the last iteration, which invokes undefined behavior.
When i == 4, s[i] and t[i] is an out of bounds access as s and t are both arrays of 4 int, not 5.
It needs to be i < 4 or i <= 3 for the arrays of 4 int or (if you want to) or change the initialization to i = 1.

How to Check for Array Duplicates in C

The aim of my C program is to take two arrays (both comprised of unique numbers) and merge the two of them into a new array, eliminating any numbers that are the same between both of them. However, when I try to merge the two, it instead prints back both arrays combined without eliminating any duplicates.
My program creates "array_C" by first adding in the elements from "array_A". Afterwards, it checks if there are duplicates between "array_B" and "array_C" using a counter variable. For every value in "array_C" that the for loop checks, if the value of "array_B" is not equal to the value in "array_C", the counter decreases by 1. If after all the values in "array_C" are checked, the counter is <= 0, that means there are no duplicates of that value in "array_C", and it should be added to the end of "array_C". I keep track of this using a "position" variable.
//Creation of array_C
int length_C = length_A + length_B;
int array_C[length_C];
//Copying array_A to array_C
for (i = 0; i < length_A; i++) {
array_C[i] = array_A[i];
}
//Checking array_C against array_B for duplicates
counter = length_A;
int position = length_A;
for (i = 0; i < length_B; i++) {
for (j = 0; j < length_C; j++) {
if (array_B[i] != array_C[j]) {
counter--;
} else {
counter++;
}
}
//this is the position tracker to add new value in array_C
if (counter <= 0) {
array_C[position] = array_B[i];
position++;
}
}
If I entered this:
Enter the length of array 1: 6
Enter the elements of the array: 1 2 3 4 5 6
Enter the length of array 2: 6
Enter the elements of the array: 3 4 5 6 7 8
I expect the results should look like this:
Here is the merged array:
1 2 3 4 5 6 7 8
But instead, it looks like this:
1 2 3 4 5 6 3 4 5 6 7 8
So apparently something is going wrong and it is not understanding that it should only add variables that are not duplicates.
Your logic is flawed. That's why you are getting unexpected outcome. See the following revision in your code:
for (i = 0; i < length_B; i++) {
int skip = 0;
for (j = 0; j < length_C; j++) {
if (array_B[i] == array_C[j]) {
skip=1;
break;
}
}
if(skip == 1) continue;
array_C[position++] = array_B[i];
}
the problem is with the logic inside your inner for loop. according to the problem statement if any value of array_c matches with any value of array_b you should get rid of that value otherwise add the value to array_c. so you can simply try doing the following. please make sure you understand the code. if you have any question feel free to ask.
for (i = 0; i < length_B; i++) {
bool isExistInArrayC = false;
for (j = 0; j < length_C; j++) {
if (array_B[i] == array_C[j]) {
isExistInArrayC = true;
break;
}
}
//this is the position tracker to add new value in array_C
if (isExistInArrayC == false) {
array_C[position] = array_B[i];
position++;
}
}
The suggestions will certainly work, but performance (especially with large size arrays) will be very poor. I would maintain a sorted array "C" and do a binary search into it when adding integers from array B.
You'll need a double-linked list for array C of course.

What needs to be modified to get desired result in this C code

I have written a small piece of code that would perform Run length encoding kind of stuff on 1-D array but still far from desired result.
main()
{
int a[8]={2,0,0,0,3,0,0,9};
int i,temp,ct=0,flag,m;
int found[90]={0};
for(i=0;i<=7;i++)
{
if(!a[i])
{
ct++;
if(!found[a[i]])
{
flag=i;
found[a[i]]=1;
}
}
}
a[flag]=ct;
m=ct;
for(i=0;i<m;i++)
{
printf("%d",a[i]);
}
}/* end of main*/
Now for above array i would like to have output something below
2 5 0 3 9
But with my piece of code am getting
2 5 0 0 3
Can I have any suggestion on that?
Shouldn't run length encoding turn 2,0,0,0,3,0,0,9 into 2 1 0 3 3 1 2 0 9 1?
1) The first thing I see is wrong is that you aren't looking at the entire array. You're using < to stop before 8, but also stopping at 7, so you only evaluate array items 0 - 6.
2) If ct stands for count it's never reset (ct=0 only on declaration). Also it's assignment is this: a[flag]= ct; which overwrites your original data. It basically tracks the value of i.
This is my version I've just put together:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int runningCount = 1; //because we start at array index 1 and not zero
for (i = 1; i <= SZ; i++) {
if (a[i - 1] == a[i]) //value same as one before it...
runningCount++;
else { // new value found. print last one, and the count of the last one.
printf("%d %d ", a[i - 1], runningCount);
runningCount = 1; //reset for next loop
}
}
return 0;
}
The output is 2 1 0 3 3 1 0 2 9 1
Ok based on the comment left below, your algorithm would actually look like this:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int zero_count = 0; //target zeros specifically...
for (i = 0; i < SZ; i++) {
if (a[i] == 0)
zero_count++;
}
//now write it out in a bizarre, unparsable format again...
for (i = 0; i < SZ; i++) {
if (a[i] != 0) //write out all non zero values
printf("%d ", a[i]);
if (i == 0) { //this says put the zero count after the first number was printed
printf("%d 0 ", zero_count); //inserting it into a strange place in the array
}
}
return 0;
}
which outputs: 2 5 0 3 9
You need a <= in your for loop:
for(i=0;i<=7;i++)
instead of
for(i=0;i< 7;i++)
Otherwise you miss the last element.
All you appear to be doing is (a) counting the number of times 0 occurs in the array, and (b) replacing the first occurrence of 0 with that count. It's not clear how this is meant to be a useful encoding.
In any case, you're not getting your desired result, at least in part, because you're only modifying one element of the array. I suspect what you want, or at least think you want, is to shift the non-zero elements of the array to the left as you encounter them.
What is the utility of compressing the array in the way you propose? Is some other piece of code going to have to reconstruct the original, and if so how do you expect to do so from your desired result?

Resources