Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
- finding the sum of numbers whose value is in the range [10; 50]
- counting the even number in the array
- finding the smallest according to the value of the number in the array
- printing of all elements that have an odd index
source code:
C++
#include<stdio.h>
#define MAX 50 // максимално възможен брой на елементите от масива
int findMinElem(int x[MAX], int n); // функция за търсене на max
int findSum(int x[MAX], int n); // функция за търсене на сума
int findEven(int x[MAX], int n);
int findOddNumbers(int x[MAX], int n);
int main()
{
	int arr1[MAX]; // едномерен целочислен масив
	int minelem, i, n,odd,feven, sum = 0;
	
	printf(" n= "); // определяне на размера на масива
	scanf_s("%d", &n);

	printf(" Vavedete %d elementa v masiva :\n", n);
	for (i = 0; i < n; i++) // въвеждане на масива
	{
		printf(" element - %d : ", i);
		scanf_s("%d", &arr1[i]);
	}
	odd = findOddNumbers(arr1, n);
		sum = findSum(arr1, n); // викане на функция за определяне на сума
	minelem = findMaxElem(arr1, n);// виканена функция за определяне на max
	odd = findOddNumbers(arr1, n);
	feven= findEven(arr1, n);
	printf(" The sum is : %d\n", sum); // извежданена резултат за сума
	printf(" The max is : %d\n", minelem); // извеждане на резултат за max
	printf(" The sum of Even is: %d\n", findEven);
	printf(" The sum of Odd is : %d\n", findOddNumbers);

	return 0;
}
int findSum(int arr1[MAX], int n)
{
	int i = 0, s = 0; // Алгоритъм за сумиране
	while (i < n)
	{
		s = s + arr1[i];
		i++;
	}
	return s;
}
int findMinElem(int arr1[MAX], int n)
{
	int i = 1, minelem; // Алгоритъм за последователно търсене
	minelem = arr1[0];
	while (i < n)
	{
		if (minelem < arr1[i])
			minelem = arr1[i];
		i++;
	}
	return minelem;
	int  findEven(int x[MAX], int n)
		int i = 0,
		int findOddNumbers(int x[MAX], int n)
		int i = 0,

}


What I have tried:

I need to find Even and Odd Numbers
Posted
Updated 10-Jan-21 5:00am
v2
Comments
Richard MacCutchan 10-Jan-21 5:06am    
Odd numbers are those that end in 1, even numbers are those that end in 0.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
What about using the debugger? Read this Debugging chapter and other parts of this tutorial to kickstart learning to debug.

Tip: it also a good idea to write some test code to make that process easier and maintain code. I do it regularly :-O
 
Share this answer
 
I'll give you one piece of help on how to distinguish between even and odd numbers - taking into account how integers are represented in binary.

You can use a boolean operator and the value 1 to tell them apart. I leave it to you to determine which boolean operator to use. When you figure that out, you'll see that like the rest of the assignment, it's a rather simple task designed to teach you some very basic programming principals you will need well into the future. It's simple, of course, because I have experience - you get through this and you, too, will have experience.


 
Share this answer
 
You may test the parity of a number by looking at the reminder of its division by 2. If the reminder is zero the the number is even, otherwise it is odd (note you may also test its Least Significant Bit (LSB): in binary base all odd numbers have the LSB set).
I've fixed your code:
C
#include<stdio.h>
#define MAX 50 // максимално възможен брой на елементите от масива

int findMin(int x[], int n); // функция за търсене на max
int findSum(int x[], int n); // функция за търсене на сума
int findEvenSum(int x[], int n);

int main()
{
  int arr[MAX]; // едномерен целочислен масив
  int min, n, even_sum, sum = 0;

  do
  {
    printf(" n= "); // определяне на размера на масива
    scanf("%d", &n);
  } while (n<1 || n>MAX);


  printf(" Vavedete %d elementa v masiva :\n", n);
  for (int i = 0; i < n; ++i) // въвеждане на масива
  {
    printf(" element - %d : ", i);
    scanf("%d", &arr[i]);
  }
  sum = findSum(arr, n); // викане на функция за определяне на сума
  min = findMin(arr, n);// виканена функция за определяне на max
  even_sum = findEvenSum(arr, n);
  printf(" The sum is : %d\n", sum); // извежданена резултат за сума
  printf(" The min is : %d\n", min); // извеждане на резултат за max
  printf(" The sum of Even is: %d\n", even_sum);
  printf(" The sum of Odd is : %d\n", (sum - even_sum));

  return 0;
}
int findSum(int arr[], int n)
{
  int sum = 0; // Алгоритъм за сумиране
  
  for (int i = 0; i < n; ++i)
    sum += arr[i];

  return sum;
}
int findMin(int arr[MAX], int n)
{
  int min = arr[0]; // Алгоритъм за последователно търсене
  for (int i = 1; i < n; ++i)
  {
    if (min > arr[i])
      min = arr[i];
  }
  return min;
}
int  findEvenSum(int arr[], int n)
{
  int even_sum = 0;
  for (int i = 0; i < n; ++i)
  {
    if ( (arr[i] % 2 == 0) ) // if the reminder of the division by 2 is zero then the number is even
    {
      even_sum += arr[i];
    }
  }
  return even_sum;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900