1

I am writing some c coding involving some array manipulation. I am trying to achieve the following:

  1. With a given input array, create a new_array consisting of only the first 10 elements
  2. Find the index_of_largest element in the new_array
  3. Take the 2 elements to the left of index_of_largest and 3 to the right and put them all in a new_array2
For example if array[15] = {12,25,99,56,44,79,62,11,10,2,1,3,4,5,6}
then new_array[10] = {12,25,99,56,44,79,62,11,10,2}
then new_array2 [6]={12,25,99,56,44,79}

This is my current code:

#include<stdio.h>  
  
  
int main()  
{  
    int a[15]={12,25,99,56,44,79,62,11,10,2,1,3,4,5,6}, arr1[10], arr2[5], i, pos=10, k1 = 0, k2 = 0, max_index, max;  
  
    for(i = 0; i < 10; i++)  
    {  
        if(i < pos)  
            arr1[k1++] = a[i];  //seperate array
        else  
            arr2[k2++] = a[i];  
    }  
  
    printf("\nElements of First Array -> arr1[%d]\n", k1);  
    for(i = 0; i < k1; i++)  
        printf("%d\n", arr1[i]); 

    max  = arr1[0]; //find index of max element
    for (i = 0; i < 10; i++)
    {
        if (arr1[i] > max) {
            max = arr1[i];
            max_index = i;
        }
    }
    printf("Largest element = %d at index %d", max, max_index);

    return 0;
    }

output:

Elements of First Array -> arr1[10]
12
25
99
56
44
79
62
11
10
2
Largest element = 99 at index 2

I am able to separate the initial array (1) am able to find the index of the largest element (2). I am not sure how to solve (3) moving forward from my current point. The largest index will always been in a position in the array so that it will be possible to get the elements to around it. (For example the largest value will not be the first or last index) I also think I should add a condition so that if there are multiple max values, we take the first one that occurs. I will in the future wrap all this in a function call but sorry for the messy code at the moment.

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    Changing the code/question after answers arrive is poor SO etiquette. Best to roll back. – chux - Reinstate Monica Sep 10 '21 at 13:55
  • You define `arr2` as an `int[5]`, but you intend to store 6 elements in it. – Stef Sep 10 '21 at 13:59
  • @Stef Sorry I don't understand, if my a[15] has elements and arr1[10] wouldn't arr2 only have 5 elements left? Unless I am getting confused about index starting at 0 for c. I won't need arr2 for anything (just made it to store the junk elements) but I'd like to understand the point you bring up. – Ashton Stark Sep 10 '21 at 14:04
  • @AshtonStark Well, your post begins by explaining that you want to copy the first 10 items in a new array, and then 2(left)+1(largest)+3(right) items from the new array to a second new array. I assumed these two new arrays were `arr1` and `arr2`. If you really just defined `arr2` to store junk elements, then you don't need `arr2` at all; just ignore junk elements. – Stef Sep 10 '21 at 14:07
  • Actually, you define `pos=10`, then have a loop with condition `i<10`, and then inside the loop `if (i < pos)`. The condition for the "if" is obviously going to be always true, so you don't need this "if" at all. – Stef Sep 10 '21 at 14:08
  • Yes you are right, my final array will have 6 elements indeed new_array2[6]. That's a good point, I will make that change for efficiency. – Ashton Stark Sep 10 '21 at 14:13
  • When you loop in arr1 to find largest element you should use I < k1 . Also the search of max element could be done in the first loop while filling arr1 (small optimization). To build arr2 you just need to add elements of arr1 from max_index - 2 to max_index + 3 into srr2. – Ptit Xav Sep 10 '21 at 15:22

1 Answers1

2

You're using %f format specifier, this is for floats and doubles. max is an int, you need to use %d or similar:

printf("Largest element = %d at index %d", max, max_index);

For more information on printf format specifiers, see the man page

yano
  • 4,827
  • 2
  • 23
  • 35
  • AH you are right, not sure how I messed that one up haha, thank you!! I will make those changes to the code above. – Ashton Stark Sep 10 '21 at 13:50
  • 2
    @AshtonStark "not sure how I messed that one up" ---> because you are not using a good compiler will all warnings enabled. Save time, enable a compiler warnings. – chux - Reinstate Monica Sep 10 '21 at 13:53
  • @Reinstate Monic That is true, no error flags or warnings were thrown out. I am using VS code, is there a better ide you would recommend? – Ashton Stark Sep 10 '21 at 13:57
  • 1
    @AshtonStark Relevant question: [How to include compiler flags in Visual Studio Code?](https://stackoverflow.com/questions/57173093/how-to-include-compiler-flags-in-visual-studio-code). I recommend you set `-Wall -Wextra -Werror` at the very least. Other flags might be interesting too, such as `-Wfloat-equal`. – Stef Sep 10 '21 at 14:01
  • Thank you for this, this will make debugging easier – Ashton Stark Sep 10 '21 at 14:14