How to use block in c - c

I have one block with integers, I need to do some task in school.
I need to group even numbers to one block (even numbers)
and i need to group odd numbers to one block (odd numbers)
I was trying to do something, but this doesn't work normally
#include <stdio.h>
#include <stdlib.h>
int main()
{
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
int parostomb[] = {};
int paratlantomb[] = {};
int n = 9;
int i = 0;
int j = 0;
int paros = 0;
int paratlan = 0;
for (i=0; i < n; i++){
if (tomb[i]%2 == 0) {
parostomb[paros] = tomb[i];
paros = paros + 1;
printf("paros: %d\n", parostomb[paros]);
}
else {
paratlantomb[paratlan] = tomb[i];
paratlan = paratlan + 1;
printf("Paratlan:%d\n", paratlantomb[paratlan]);
}
}
return 0;
}
Please help
If you run the code you will be see the code is doesn't work normally

Here
int parostomb[] = {};
int paratlantomb[] = {};
you do not set the array size.
You need to change it to:
int parostomb[sizeof(tomb)/sizeof(int)] = { 0 };
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0 };
So what is sizeof (tomb)/sizeof(int) doing? It calculates the number of elements in the array tomb. The first part sizeof(tomb) calculates the number of bytes in the array. The second part sizeof(int) calculates the number of bytes in an int. Consequently, diving the first by the second will give you the number of int in the array (aka the number of array elements).
sizeof(tomb)/sizeof(int) can also be written sizeof tomb/sizeof *tomb. The later is typically considered the best because in case you to change the array type, e.g. use long int instead of int, you don't have to change sizeof(int) to sizeof(long int). When using sizeof *tomb the actual type is handled automatically.
Further here:
parostomb[paros] = tomb[i];
paros = paros + 1;
printf("paros: %d\n", parostomb[paros]);
You increment paros before printing so you do not print the newly assigned value.
Change to:
parostomb[paros] = tomb[i];
printf("paros: %d\n", parostomb[paros]);
paros = paros + 1;
Further here:
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
...
int n = 9; <-------------------- The array size is only 8
To avoid such bugs do:
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
...
int n = sizeof(tomb)/sizeof(int);
Putting it all together, your code should be:
int main(void)
{
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
int parostomb[sizeof(tomb)/sizeof(int)] = {0};
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0};
int n = sizeof(tomb)/sizeof(int);
int i = 0;
int paros = 0;
int paratlan = 0;
for (i=0; i < n; i++){
if (tomb[i]%2 == 0) {
parostomb[paros] = tomb[i];
printf("paros: %d\n", parostomb[paros]);
paros = paros + 1;
}
else {
paratlantomb[paratlan] = tomb[i];
printf("Paratlan:%d\n", paratlantomb[paratlan]);
paratlan = paratlan + 1;
}
}
}
Output
Paratlan:5
paros: 10
paros: 8
Paratlan:3
Paratlan:-1
paros: 4
paros: 2
Paratlan:9
That said... It seems strange that you save the numbers into two arrays that you never really use. It would make more sense to generate the arrays first and then print them in separate loops. Like:
int main(void)
{
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
int parostomb[sizeof(tomb)/sizeof(int)] = {0};
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0};
int n = sizeof(tomb)/sizeof(int);
int i = 0;
int paros = 0;
int paratlan = 0;
for (i=0; i < n; i++){
if (tomb[i]%2 == 0) {
parostomb[paros] = tomb[i];
paros = paros + 1;
}
else {
paratlantomb[paratlan] = tomb[i];
paratlan = paratlan + 1;
}
}
printf("Even numbers: ");
for (i=0; i < paros; i++){
printf("%d ", parostomb[i]);
}
printf("\n");
printf("Odd numbers: ");
for (i=0; i < paratlan; i++){
printf("%d ", paratlantomb[i]);
}
printf("\n");
}
Output
Even numbers: 10 8 4 2
Odd numbers: 5 3 -1 9

Related

My code doesn't returns an output. It returns only exit code

I wanted to reverse half of the arrays inputs with the other half.
#include <stdio.h>
#include <math.h>
void main() {
double size = 5;
int array[5] = {1, 2, 3, 4, 5};
int half_size = ceil(size / 2);
for(int i = 0; i < half_size; i++){
int a;
int rev = size - (i + 1);
array[i] = a;
array[i] = array[rev];
array[rev] = a;`enter code here`
}
printf("%d", array[5]);
}
I agree with #Eugene Sh.'s and #FredK's suggestions. The line array[5] in the line printf("%d", array[5]); is out of bound since array only have indexes from 0 to 4. Since I assume you want to print out the last element in the array, you should change it to printf("%d", array[4]);. Another thing is that your assignment expression array[i] = a; is wrong. I assume the expression is part of the swapping process from element in index i with element in index rev. If that was the case then you should change it to a = array[i]; instead. I update you code according to my suggestion and it outputs the correct result. I added the for loop to verify that the array values are reversed for testing purpose. You can delete it after you're done testing.
#include <math.h>
int main() {
double size = 5;
int array[5] = {1, 2, 3, 4, 5};
int half_size = ceil(size / 2);
for(int i = 0; i < half_size; i++){
int a;
int rev = size - (i + 1);
a = array[i];
array[i] = array[rev];
array[rev] = a;
}
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
printf("%d", array[4]);
}

how to return indexes that makes best sum in an array?

I just need to input an array and make the program return the range of index which produces highest sum.
Examples
For A = {5, -6, 7, -5, 10, -1} – the best sum consists of the values in A[2..4] for a total of 7 + -5 + 10 = 12
For A = {1, 2, 4, -6, 4, 2, 1} – the best sum consists of the values in A[0..6] (the entire array) for a total of 8
For A = {-5, 2, -3, 1, -5, 4, -2} – the best sum consists of the value in A[5] for a total of 4
It would be greatly appreciated if someone could help me return the following values
Here is my code(doesn't seem to run properly)
#include<stdio.h>
int the_resource_collection( int arr[], int len){
int highest, start, end, i, result;
highest = arr[len-1];// the limits of the highest value
start = len - 1;
end = len - 1;// all the variables in the total indexes
for( i=0; i<len-2; i++)
{
for (a=i; a<len-1; a++)
{
result= arr[i]+ arr[a];// try to find the total of the index
{
if (result > highest)
{
highest = result;
start = i;
end = a;
}
}
}
}
return start;
return end;
}
int main()
{
int a[6] = {5, -6, 7, -5, 10, -1};
int length = 6;
printf("%d",the_resource_collection(&a[6],length));
}
Ok, you have some mistakes.
Firstly, in printf("%d",the_resource_collection(&a[6],length)); you are only passing to the function the sixth element of the array.
Also your for loops aren't considering all elements of the array. Finally you also need to save the begining and ending in a int array.
Here is how I would do it (I've tested it with the 3 inputs you've provided and the output is as expected!):
#include<stdio.h>
int * the_resource_collection( int arr[], int len){
int highest;
int returnvalue[2];
int i;
int a;
int result;
highest = arr[len-1];// the limits of the highest value
returnvalue[0] = len - 1;
returnvalue[1] = len - 1;// all the variables in the total indexes
for( i=0; i<len; i++)
{
result= arr[i];
for (a=i+1; a<len; a++)
{
if (result > highest)
{
highest = result;
returnvalue[0] = i;
returnvalue[1] = a-1;
}
result+=arr[a];
}
if (result > highest)
{
highest = result;
returnvalue[0] = i;
returnvalue[1] = a-1;
}
result=0;
}
return returnvalue;
}
int main()
{
int a[7] = {5, -6, 7, -5, 10, -1};
int length = 7;
printf("A[%d...%d]\n",the_resource_collection(a,length)[0],the_resource_collection(a,length)[1]);
}
If you don't understand what I wrote, or need anymore help, please say so.

How to return largest two numbers in array in C?

So, I have this so far. I'm trying to find the two largest numbers in an array and return them. I looked up a lot of resources online, and most of them say "call by reference" is the way to go. But I've no idea how to make it work with my program. For example, I saw this example online:
void Calculate(int x, int y, int* prod, int* quot)
{
*prod = x*y;
*quot = x/y;
}
int x = 10,y = 2, prod, quot;
Calculate(x, y, &prod, &quot)
How does the above program actually "return"? How do I print the return values to the console?
#include "stdio.h"
void largest_two( int numbers[], int len, int *largest, int *next_largest){
int i, temp;
*largest = numbers[0];
*next_largest = numbers[1];
if(*largest < *next_largest){
temp = *next_largest;
*largest = *next_largest;
*next_largest = temp;
}
for (i=0; i<sizeof(numbers); i++) {
if(numbers[i]>= *largest){
*largest = numbers[i];
*next_largest = *largest;
}
else if ( numbers[i] > *next_largest){
*next_largest = numbers[i];
}
}
}
int main() {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int len = 3;
int largest, next_largest;
//==>??? printf("%d %d", largest_two(numbers, len, &largest, &next_largest));
}
Sides' from the pointer issues (you should read a tutorial / book on them), your main problem is that you're attempting to print the single return value of a function with return type void which means it won't return at all.
Your code:
int main() {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int len = 10; // sizeof(numbers)
int largest, next_largest;
largest_two(numbers, len, &largest, &next_largest);
printf("%d %d", largest, next_largest);
}
Keep in mind this is still not entirely correct, but it does adress your problem of printing the numbers.
Also, passing len means you shouldn't do this for (i=0; i<sizeof(numbers); i++) but this instead for (i=0; i<len; i++)
Firstly, this line:
for (i=0; i<sizeof(numbers); i++)
is not correct. You want this to be instead:
for (i=0; i<len; i++)
which should be passed to largest_two() as sizeof numbers/sizeof numbers[0], which is the actual length of the array.
I also suggest setting largest and next_largest to INT_MIN from <limits.h>, and then finding these values from their. It seems you are also having trouble with pointers, and it would be best to use them only when needed.
Here is an example which simplifies your approach, which finds the largest and second largest element in one loop of the array. It also only uses pointers when needed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAYSIZE(x) (sizeof x/sizeof x[0])
void largest_two(int numbers[], size_t len, int *largest, int *next_largest);
int main(void) {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int largest, next_largest;
largest_two(numbers, ARRAYSIZE(numbers), &largest, &next_largest);
printf("largest = %d\nnext_largest = %d\n", largest, next_largest);
}
void largest_two(int numbers[], size_t len, int *largest, int *next_largest) {
int max, smax;
max = smax = INT_MIN;
for (size_t i = 0; i < len; i++) {
if (numbers[i] > max) {
smax = max;
max = numbers[i];
} else if (numbers[i] > smax && numbers[i] < max) {
smax = numbers[i];
}
}
*largest = max;
*next_largest = smax;
}
Output:
largest = 8
next_largest = 6
Second dataset:
int numbers[] = {3, 1, 6, 3, 6, 2, 8, 0, 8, 7};
Output:
largest = 8
next_largest = 7

Find count of missing elements in an unsorted array of integers

How can I find how many elements are missing from an array of integers in C, if some of the numbers are duplicated?
Assume the array is int array = {1, 2, 1, 5, 4} and there should be numbers up to 6. Then, the program/function should output/return 2, as there are 2 elements missing (3, 6).
Note: 0 is not counted as a missing number, nor can it be present in an array.
This way?
int countMissing(int *x, int arrLen, int bound)
{
int * y = malloc ((bound + 1) * sizeof(int));
int i = 0;
int missing = 0;
memset(y,0,sizeof(int)*(bound+1));
for(i = 0; i<arrLen; i++)
{
if(x[i]<=bound)
{
y[x[i]] = 1;
}else
{
// error handling e.g.
return -1;
}
}
for(i = 1; i<=bound; i++)
{
if(y[i]==0) missing++;
}
free(y);
return missing;
}
Usage:
int main(void)
{
int array [] = {1, 2, 1, 5, 4};
printf("%d", countMissing(array, 5, 10));
return 0;
}
Output: 6.

How to find top 6 elements in an array in C

I am trying to find top 6 elements from an array with their ordering number.
int x=0;
for (int k = 0; k < 6; k++) //
{
for (i = 1; i <= 90; i++)
{
if (sorted[k] < holder[i] && i >= x)
{
sorted[k] = holder[i];
x = i; //
}
}
}
But this does not work. I want it to give me output like 43->7 15 ->3 etc..
Haven't written C in a while, but here is a simple solution that modifies the array in place and uses selection sort to select the k highest numbers in the array and moves them to the front. It keeps an array of indices that correspond to where the number originally was and applies the same swaps to it.
#include <stdio.h>
#define ELEMENTS 10
void main(void)
{
// example input for execution
int numbers[10] = {9,4,5,1,8,2,3,6,0,7};
// tracks ordering of indices
int indexes[10] = {0,1,2,3,4,5,6,7,8,9};
int k = 6;
int i, j;
int max, temp;
// Partial selection sort, move k max elements to front
for (i = 0; i < k; i++)
{
max = i;
// Find next max index
for (j = i+1; j < ELEMENTS; j++)
{
if (numbers[j] > numbers[max]) {
max = j;
}
}
// Swap numbers in input array
temp = numbers[i];
numbers[i] = numbers[max];
numbers[max] = temp;
// Swap indexes in tracking array
temp = indexes[i];
indexes[i] = indexes[max];
indexes[max] = temp;
}
for (i = 0; i < k; i++) {
printf("%d -> %d\n", indexes[i], numbers[i]);
}
}
And the output:
0 -> 9
4 -> 8
9 -> 7
7 -> 6
2 -> 5
1 -> 4
Here's the answer I have for you.
I would love some constructive criticism on it from anyone who can provide some.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int *ptrNumbers[10];
int i=0;
for(; i < 10; i++){
ptrNumbers[i] = &numbers[i]; // assign the addresses
}
int topSix[6];
int topSixIndex=0;
for(; topSixIndex < 6; topSixIndex++){
int **best = NULL; // Pointer to the pointer to the value.
int checkIndex=0;
for(; checkIndex < 10; checkIndex++){
if(ptrNumbers[checkIndex] != NULL){
if(!best){
/* best is not yet defined */
best = &ptrNumbers[checkIndex];
// best now points to the pointer for numbers[checkIndex]
}else if(*ptrNumbers[checkIndex] > **best){
// this else if statement could be attached to the main if as
// an or condition, but I've separated it for readability.
best = &ptrNumbers[checkIndex];
// best now points to the pointer for numbers[checkIndex]
}
}
}
// assign the topSix position and flag the ptrNumbers
topSix[topSixIndex] = **best;
*best = NULL;
}
// now we'll print the numbers
for(topSixIndex = 0; topSixIndex < 6; topSixIndex++){
printf("%d\n", topSix[topSixIndex]);
}
return 0;
}
Essentially the program works like this: Given an array of ten numbers, a second array is constructed to house pointers to those 10 numbers. A third array is then constructed to house the values of the top 6 numbers. A for loop is then initialized to loop 6 times to find the highest unrecorded value. When the highest value is found by looping the pointer array, the value is assigned to the next index of the top six array. Once that value is added, the pointer array's index that points to the top six value is then assigned to NULL. This acts as a flag insuring that the value will not be added again. Finally, all numbers are printed out.
After running this code, the output I received was:
9
8
7
6
5
4
Edit: as a note, the ordering number's can be stored in a second array. You would simply need to track the checkIndex of the highest value and then assign it to a second array which contained the index values.
maybe you aren't looking for a code-only answer, but this will work:
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// return index of max element
int max_index( int* vec, int sz )
{
int idx, max, i;
if(!sz) return -1;
idx = 0;
max = vec[0];
for(i=1; i<sz; ++i)
{
if( vec[i] > max )
{
max = vec[i];
idx = i;
}
}
return idx;
}
// return indexes of N top elements
void top( int* vec, int sz, int* out_vec, int N )
{
int i, *tmp, idx;
tmp = (int*) malloc( sz*sizeof(int) );
memcpy( tmp, vec, sz*sizeof(int) );
for(i=0; i<N; ++i )
{
idx = max_index(tmp,sz);
out_vec[i]=idx;
tmp[idx] = INT_MIN;
}
free(tmp);
}
see it live here
Make an array of struct that contain data and index, then sort it and pick up first or last 6 elements to output.
Say that you are given an array numbers. Then create an array indexes with the same size as numbers in such a way that its values are equal to their indexes. Here is an illustration:
numbers = [ 1, 7, 3, 9, 2, 0 ]
indexes = [ 0, 1, 2, 3, 4, 5 ]
Sort numbers in descending order, performing the same operations on indexes. In the end, you should end up with something like this:
numbers = [ 9, 7, 3, 2, 1, 0 ]
indexes = [ 3, 1, 2, 4, 0, 5 ]
Finally, all you need to do is work with the first six elements of these arrays.
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int contains(int array[], int array_size, int value)
{
int i;
for (i = 0; i < array_size; i++)
{
if (array[i] == value)
{
return TRUE;
}
}
return FALSE;
}
int main()
{
int numbers[] = { 1, 7, 3, 9, 2, 0 };
int indexes[] = { 0, 1, 2, 3, 4, 5 };
int numbers_size = 6;
int largest[] = { -1, -1, -1, -1, -1, -1 };
int largest_index = 0;
int i;
for (i = 0; i < 6; i++)
{
int j;
int max_index = -1;
int max = -2147483648;
for (j = 0; j < numbers_size; j++)
{
if (numbers[j] >= max && contains(largest, numbers_size, j) == FALSE)
{
max_index = j;
max = numbers[max_index];
}
}
largest[largest_index++] = max_index;
}
for (i = 0; i < 6; ++i)
{
printf("%d->%d\n", largest[i], numbers[largest[i]]);
}
return 0;
}
You probably should use bubblesort (and keep a function holding all the original indexes) and then just make it show the 6 first number of both arrays (from the indexes array and from the array you sorted itself)

Resources