print evens number and odds number from an array - c

Declare an array containing these number and print the evens numbers and odd numbers
Now I initialized an array that containing 11 integers.
Here is my code
#include <stdio.h>
int main(void) {
int nums[11] = {11,3,9,7,6,10,13,17,2,8,3}; // create an variables that store integers
int evens[11] = {0}; // initialized an array to store even numbers
int odds[11] = {0}; // initialized an array to store even numbers
int length = sizeof(nums) / sizeof(nums[0]); // get the length of nums
int nums_index = 0;
int evens_index = 0;
int odds_index = 0;
for (nums_index; nums_index < length;nums_index++) {
if (nums[nums_index] % 2 == 0) {
evens[evens_index] = nums[nums_index];
evens_index++;
}
else if(nums[nums_index] % 2 != 0) {
odds[odds_index] = nums[nums_index];
odds_index++;
}
printf("%d\n",evens[evens_index]);
printf("%d\n",odds[odds_index]);
}
return 0;
}
The major question is whether the output has problems when I compile my code.
The output is :0 11 0 3 0 9 0 7 6 0 10 0 0 13 0 17 2 0 8 0 0 3
Why it could happened?
Thank you all.

You need separate indexing for each array, advancing the index for evens and odds only when nums[i] value is one of the two.
Otherwise you would get sort of a copy of nums with zeroes in place of those numbers of the opposite type (odd/even).
For instance:
int j = 0;
int k = 0;
for (int i = 0; i < length; i++) {
if (nums[i] % 2 == 0) {
evens[j] = nums[i];
j++;
}
else if(nums[i] % 2 != 0) {
odds[k] = nums[i];
k++;
}
printf("%d\n",evens[i]);
printf("%d\n",odds[i]);
}
This will compose the arrays like:
11 3 9 7 13 17 3 0 0 0 0 --- for odds
6 10 2 8 0 0 0 0 0 0 0 0 --- for evens
The second problem is that you are printing inside the loop, firstly a value from evens and immediately after a value for odds.
So if you want to display them nice and separate, you can move both printf outside the first loop, then looping again on each result array for displaying it completely, before proceding to the other.

#include <stdio.h>
void PrintNumbers(int*, int);
int main(void) {
int nums[11] = {11,3,9,7,6,10,13,17,2,8,3}; // create an variables that store integers
int evens[11] = {0}; // initialized an array to store even numbers
int odds[11] = {0}; // initialized an array to store even numbers
int length = sizeof(nums) / sizeof(nums[0]); // get the length of nums
int nums_index = 0;
int evens_index = 0;
int odds_index = 0;
for (nums_index; nums_index < length; nums_index++)
{
if (nums[nums_index] % 2 == 0)
{
evens[evens_index] = nums[nums_index];
evens_index++;
}
else if(nums[nums_index] % 2 != 0)
{
odds[odds_index] = nums[nums_index];
odds_index++;
}
}
printf("Original List: ");
PrintNumbers(nums, length);
printf("Even numbers: ");
PrintNumbers(evens, length);
printf("Odd numbers: ");
PrintNumbers(odds, length);
return 0;
}
void PrintNumbers(int* numbers, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d, ", numbers[i]);
}
printf("\n");
}
Output:
Original List: 11, 3, 9, 7, 6, 10, 13, 17, 2, 8, 3,
Even numbers: 6, 10, 2, 8, 0, 0, 0, 0, 0, 0, 0,
Odd numbers: 11, 3, 9, 7, 13, 17, 3, 0, 0, 0, 0,

Related

This question is regarding A C program to replace the second the subsequent occurences of the digit with 0,-1,-2... so on so if 1,1,1 -> 1,0,-1

#include <stdio.h>
int main()
{
int a[]={1,2,1,2,1,2,1,2,1};
int count=1;
int size=sizeof(a)/sizeof(a[0]);
for(int k=0;k<=size;k++){
for(int i=0;i<=size;i++){
count = 1;
for(int j=i+1;j<=size;j++){
if(a[j]==a[i]){
count--;
a[j]=count
}
} }}
I tried like just two loops,and using a count variable to decrease the value to get the output like 1,2,0,0,-1,-1 for the array 1,2,1,2,1,2 as 1 and 2 repeating 3 three times but Im getting error with Second digit ie '2' im getting 2,0,0,0... like 1 2 0 0 -1 0 -2 0 -3.
You have too many loops, and invoke Undefined Behaviour by accessing the element one-past the end of the array.
If the array is being changed in place, you must account for the fact that values less than 1 will hold special meaning as marked duplicates. In this case all values in the array must be greater than zero beforehand.
Otherwise, elements already marked as duplicates will be used as candidates to check for more duplicates.
For example, with the data:
1, 2, 1, 2, 1, 2
| |
After the first two elements are checked the array will be
1, 2, 0, 0, -1, -1
|
When the third element is checked, the fourth element will be changed to a zero (although its already a zero).
When the fifth element is checked
1, 2, 0, 0, -1, -1
|
the sixth element will be changed to a zero, resulting in:
1, 2, 0, 0, -1, 0
So values less than 1 must be excluded from the duplicate sweep, or a copy of the array must be used.
#include <stdio.h>
void print_array(int *data, size_t length)
{
for (size_t i = 0; i < length; i++)
printf("%2d ", data[i]);
putchar('\n');
}
int main(void)
{
int list[] = { 1, 2, 1, 2, 1, 2, 1, 2, 1 };
size_t length = sizeof list / sizeof *list;
print_array(list, length);
for (size_t i = 0; i < length; i++) {
if (list[i] > 0) {
int rep = 0;
for (size_t j = i + 1; j < length; j++) {
if (list[j] == list[i]) {
list[j] = rep--;
}
}
}
}
print_array(list, length);
}
1 2 1 2 1 2 1 2 1
1 2 0 0 -1 -1 -2 -2 -3

Trying to write a program that counts the amount of even numbers in an array and returns the value

I am trying to make a program that will count the number of even numbers in the provided arrays. When I run the program now, it will return the amount of numbers in the array, but not the amount of even numbers. For some reason my count_even function doesn't work. Can anyone help?
#include <stdio.h>
int main()
{
int data_array_1[] = { 1, 3, 5, 7, 9, 11 };
int data_array_2[] = { 2, -4, 6, -8, 10, -12, 14, -16 };
int data_array_3[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
int data_array_4[] = { 6, 2, 4, 5, 1, -9 };
int data_array_5[] = { 1, 3, 9, 23, 5, -2, 4 };
int result_1 = count_even(data_array_1, 6);
printf("data_array_1 has %d even numbers.\n", result_1);
int result_2 = count_even(data_array_2, 8);
printf("data_array_2 has %d even numbers.\n", result_2);
int result_3 = count_even(data_array_3, 11);
printf("data_array_3 has %d even numbers.\n", result_3);
int result_4 = count_even(data_array_4, 6);
printf("data_array_4 has %d even numbers.\n", result_4);
int result_5 = count_even(data_array_5, 7);
printf("data_array_5 has %d even numbers.\n", result_5);
return 0;
}
int count_even(int* data_array, int size)
{
int even_num = 0;
for (int i = 0; i == size; i++)
{
if (data_array[size] % 2 == 0)
{
even_num++;
}
}
return even_num;
}
The condition in your for loop is wrong.
The correct condition should say "as long as the index is smaller than size", but yours say "as long as the index equal to to size".
The condition should be i < size.
As for the result, it seems like it should return 0 (for the non-working code), not size.
Also, you are using size as an index, when you should use i.
In your count_even function, you are using the size attribute as the array index, when it should be i
int count_even(int* data_array, int size)
{
int even_num = 0
for(int i = 0; i <= size, ++i)
{
if(data_array[i] % 2 == 0)
{
even_num++;
}
}
return even_num;
}
these two lines are the root of the problems in the code:
for (int i = 0; i == size; i++)
{
if (data_array[size] % 2 == 0)
the for() statement, should be:
for (int i = 0; i < size; i++)
so the loop exits when reaching the end of the array
the if() statement is always looking at the same entry beyond the end of the array, This is undefined behaviour
The if() statement should be:
if (data_array[i] % 2 == 0)
However, the modulo operator & is not a good choice for negative numbers
a better choice would be:
if ( !(data_array[i] & 1 ) )

C - Recursive, cumulative sum of an array

I've been tasked with making a recursive function that takes an array of numbers, and turns it into an array of the cumulative sum of all the numbers up to this point, thus:
1, 2, 3, 4, 5 becomes 1, 3, 6, 10, 15
This is what I came up with:
#include <stdio.h>
int cumul(int tab[], int length, int ind) {
if (ind > 0) {
tab[ind] += tab[ind-1];
}
if (ind < length) {
cumul(tab, length, ind+1);
}
return 0;
}
int main() {
int ind;
int tab[6] = {1, 2, 3, 4, 5, 6};
int length = sizeof(tab)/sizeof(tab[0]);
for (ind = 0; ind < length; ind++) {
printf("%d ", tab[ind]);
}
printf("\n");
cumul(tab, length, 0);
for (ind = 0; ind < length; ind++) {
printf("%d ", tab[ind]);
}
printf("\n");
return 0;
}
It works well in most cases but I've hit a snag for oddly specific arrays:
For example, it doesn't work for tab[6] = {1, 2, 3, 4, 5, 6}, here's the output:
1 2 3 4 5 6
1 3 6 10 15 21 27 7 4196016 0 -1076574208 32528 -1609083416 32767 -1609083416 32767 0 1 4195802 0 0 0 -1815242402 30550560 4195424 0 -1609083424
I have no idea why it goes bonkers. It works fine for just about any tab[5] and tab[7] arrays I tried, but fails for every tab[6] array I tried.
The problem occurs when ind reaches length-1. For example, if length is 6, and ind is 5, then the recursive call is
cumul(tab, 6, 6); // length=6 ind+1=6
At the next level of recursion, after the if ( ind > 0 ), the code will do this
tab[6] += tab[5]; // ind=6 ind-1=5
That results in undefined behavior because you're writing beyond the end of the array.
You could check the upper bound in the first if statement, e.g.
if ( ind > 0 && ind < length )
But it's better to just avoid the recursive call by changing the second if statement to
if ( ind < length - 1 )
Either change avoids the situation where you access tab[length].

Simple c array calculator drops the first number

This is part of my assignment. I have to make a calculator using arrays.
this is the addition part of it.
It works fine but the problem is, it drops the first digit. array size is for example 10 . it doesn't show the first digit whenever addition has remainder.
for example i want to add 5 9 7 5 3 1 0 0 0 0 with 5 0 8 6 4 2 0 0 0 0. the true answer is 11061730000 but this program shows 1061730000 (drops first 1).
how do i fix this behavior??
code:
#include <stdio.h>
#define SIZE_MAX 10
#define SIZE_USE SIZE_MAX-1
int main()
{
int i;
int inum_firstPTR[SIZE_MAX] = {5, 9, 7, 5, 3, 1, 0, 0, 0, 0};
int inum_secondPTR[SIZE_MAX] = {5, 0, 8, 6, 4, 2, 0, 0, 0, 0};
int add_resultPTR[SIZE_MAX] = {0}; //initializing result array
for (i = SIZE_USE; i >= 0; i--)
{
if (add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] < 10)
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
else
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] - 10;
inum_firstPTR[i - 1] = inum_firstPTR[i - 1] + 1;
}
}
puts("");
for (i = 0; i < SIZE_MAX; i++)
{
printf("%d", add_resultPTR[i]);
}
puts("");
return 0;
}
for (i = SIZE_USE; i >= 0; i--)
{
if (add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] < 10)
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
else //carry
{
if ( i > 0) // normal carry to next element
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] - 10;
inum_firstPTR[i - 1] = inum_firstPTR[i - 1] + 1;
}
else // element [0], no more elements to carry to
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
}
}
addition of 5+5+carry1 is 11 but add_resultPTR[0] can only store 1 and there is no position before [0] to store carry1 therefore it only prints from 1.
moreover, 11061730000 are 11 digits how do you suppose to fit into an array of SIZE_MAX 10.
define SIZE_MAX as 11 and then to prevent from overflow store addition of inum_firstPTR[SIZE_MAX] and inum_firstPTR[SIZE_MAX] from add_resultPTR[0] to add_resultPTR[10] and print it in reverse i.e. if add_resultPTR[10] is 1 then from [10] to [0] otherwise from [9] to [0].

How do I compute the number of valleys in a sequence of numbers?

Given a sequence of digits, a valley is defined as the region in the sequence that is surrounded (to the left and right) by higher values. The task is to find the number of valleys in the sequence.
For example,
{9,8,7,7,8,9} has one valley at {7,7}
{9,8,7,7,8,6,9} has two valleys at {7,7} and {6}
{7,8,9,8,7} has no valleys
The code I have to compute the number of valleys is as follows:
#include <stdio.h>
#define SIZE 40
int main()
{
int input;
int store[SIZE];
int i = 0;
int j;
int valley = 0;
int count = 0;
printf("Enter sequence: ");
scanf("%d", &input);
while(input != -1)
{
store[i] = input;
i++;
scanf("%d", &input);
}
count = count + i;
for(i = 1; i < count; i++)
{
for(j = i; j < i + 1; j++)
{
if((store[j-1] > store[j]) && (store[j] < store[j+1]))
{
valley = valley + 1;
break;
}
}
}
printf("Number of valleys: %d", valley);
return 0;
}
I am able to display the correct answer if the input is "3 2 1 2 3". However, if in between the number is equal to another and they are side by side (for example, "3 1 1 2"), the program will compute the wrong answer.
How do I go about writing the program so that I am able to display the correct number of valleys?
Look for slope changes from down to up.
Rather than a double nested for loop, march along looking for slope changes from down to up. Consider any slope of 0 to be the same as the previous slope.
size_t Valley(const int *store, size_t count) {
size_t valley = 0;
int slope = -1;
size_t i;
// Find first down slope
for (i = 1; i < count; i++) {
if (store[i] < store[i - 1]) {
break;
}
}
for (; i < count; i++) {
int newslope = (store[i] > store[i - 1]) - (store[i] < store[i - 1]);
// Loop for slope changes
if (newslope == -slope) {
if (newslope > 0)
valley++;
slope = newslope;
}
}
return valley;
}
Test code.
void Vtest(const int *store, size_t count) {
size_t n = Valley(store, count);
printf("%zu %zu\n", count, n);
}
void Vtests(void) {
int a1[] = { 9, 8, 7, 7, 8, 9 };
Vtest(a1, sizeof a1 / sizeof a1[0]);
int a2[] = { 9, 8, 7, 7, 8, 6, 9 };
Vtest(a2, sizeof a2 / sizeof a2[0]);
int a3[] = { 7, 8, 9, 8, 7 };
Vtest(a3, sizeof a3 / sizeof a3[0]);
int a4[] = { 3, 2, 1, 2, 3 };
Vtest(a4, sizeof a4 / sizeof a4[0]);
int a5[] = { 8, 7, 7, 8, 6 };
Vtest(a5, sizeof a5 / sizeof a5[0]);
}
int main(void) {
Vtests();
return 0;
}
Output
6 1
7 2
5 0
5 1
5 1
The problem is here:
if((store[j-1] > store[j] )&&(store[j] < store[j+1]))
In both comparations you are using index j, so this program finds only valleys with length 1. Try this modification:
if((store[i-1] > store[i] )&&(store[j] < store[j+1]))
Also I am not sure, that it is right to break; in this situation. But it is not clear now, which answer is correct in case 3 1 2 3 - one (1) or two (1 and 1 2). From your first example we can see, that right answer is one, but it is not obvious from the definition.
Depending on whether you define valley as a higher value to the IMMEDIATE left/right of a given point you may need to adjust the Valley function provided by chux as follows:
size_t Valley (const int *store, size_t count) {
...
i++;
for (; i < count; i++) {
int newslope = (store[i] > store[i - 1]) - (store[i] < store[i - 1]);
if (newslope == -slope) {
if (newslope > 0)
valley++;
}
slope = newslope;
}
...
}
output:
$ ./bin/valleyt
6 0
7 1
5 0
5 1
5 0
This is a supplement to the answer provided by chux, and the input data is as he provided in his answer. This code just limits the definition of a valley to being created by 3 adjacent points. (a special case of the general answer of a change from negative to positive slope with intervening equivalent points)

Resources