I want to know this situation.
when I define this sentence
struct soccer team[100] ;
I can do qsort ;
qsort(team, MAX , sizeof(team[0]) , compare) ;
int compare(const void *a, const void *b )
{
SOC *A1 = (SOC*)a ;
SOC *B1 = (SOC*)b ;
if( A1->score > B1->score )
return -1 ;
else if ( A1->score == B1->score )
return 0 ;
else
return 1 ;
}
When I do dynamic allocation
struct soccer*team[MAX] ;
team[Index] = (SOC*)malloc(sizeof(SOC)) ;
error is existed. (qsort and compare is same )
I want to know how do use it(qsort for dynamic allocation struct)
please!
example ( when I use first way)
Man 3 1 1 16
Che 2 2 2 8
Asn 0 6 0 6
hot 6 0 0 18
City 0 0 6 0
Bar 1 5 0 8
is converted
hot 6 0 0 18
Man 3 1 1 16
Che 2 2 2 8
Bar 1 5 0 8
Asn 0 6 0 6
City 0 0 6 0
The first version
struct soccer team[100] ;
and the second one
struct soccer*team[MAX] ;
team[Index] = (SOC*)malloc(sizeof(SOC)) ;
are not same. The first one is an array of struct soccer and the second one is an array of struct soccer *. They are not just the same.
If you want to use the later version (including pointer) and get the same behaviour as above, you can do something like
struct soccer * team;
team = malloc(sizeof *team * SIZE) ; // SIZE is the number of elements
The same comparision function cannot be used for different element types. Use correct comparision function like this (pointers to elements, which are pointers, will be given, so dereference them to get the pointers to structs):
int compare2(const void *a, const void *b )
{
SOC *A1 = *(SOC**)a ;
SOC *B1 = *(SOC**)b ;
if( A1->score > B1->score )
return -1 ;
else if ( A1->score == B1->score )
return 0 ;
else
return 1 ;
}
Note: They say you shouldn't cast the result of malloc() in C.
Here is a demonstrative program that shows how a similar array can be sorted.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10
typedef struct soccer
{
unsigned int score;
} SOC;
int cmp( const void *a, const void *b )
{
const SOC *lhs = *( const SOC ** )a;
const SOC *rhs = *( const SOC ** )b;
return ( lhs->score > rhs->score ) - ( rhs->score > lhs->score );
}
int main( void )
{
SOC * team[MAX];
srand( ( unsigned int )time( NULL ) );
for ( int i = 0; i < MAX; i++ )
{
team[i] = malloc( sizeof( SOC ) );
team[i]->score = rand() % MAX;
}
for ( int i = 0; i < MAX; i++ )
{
printf( "%u ", team[i]->score );
}
printf( "\n" );
qsort( team, MAX, sizeof( SOC * ), cmp );
for ( int i = 0; i < MAX; i++ )
{
printf( "%u ", team[i]->score );
}
printf( "\n" );
for ( int i = 0; i < MAX; i++ ) free( team[i] );
return 0;
}
The program output is
2 7 2 5 1 6 1 5 0 4
0 1 1 2 2 4 5 5 6 7
Related
I am trying to write a recursive function for the formula:
a(n)=a(n-1)+a(n-2)
I've tried to simply write it out:
unsigned int ladder(unsigned int n)
{
unsigned int ret=0;
if (n < 1)
return ret;
ret = ladder(n - 1) + ladder(n - 2);
}
but it goes into stack overflow when calling for ladder(n-2)
(for some reason it sets n as a very large integer)
I feel like I'm missing something very basic but can't figure out what.
The function returns nothing in case when n is not less than 1.
Also when n is equal to 1 then the expression n - 2 yields the maximum value that can be stored in object of the type unsigned int.
The function can be declared and defined the following way
unsigned long long int ladder( unsigned int n )
{
return n < 2 ? n : ladder( n - 1 ) + ladder( n - 2 );
}
Here is a demonstration program.
#include <stdio.h>
unsigned long long int ladder( unsigned int n )
{
return n < 2 ? n : ladder( n - 1 ) + ladder( n - 2 );
}
int main( void )
{
for ( unsigned int i = 0; i < 25; i++ )
{
printf( "%llu ", ladder( i ) );
}
putchar( '\n' );
}
The program output is
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
I can't fix the logical error because I don't know what is wrong in this code. Every input, it shows "element not found". I would really appreciate it if someone can help me in this. Also in this code, I have assumed we'll be taking the size of the array as an odd number, what to do if we decide to take an even number as size?
#include<stdio.h>
int main(){
int size;
printf("Enter the number of elemets(odd number) : ");
scanf("%d",&size);
int arr[size];
printf("Enter the elements in ascending order : ");
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
int element;
int flag=0;
printf("Enter element to be found : ");
scanf("%d",&element);
int low=0;
int high=size-1;
while(low<high){
int mid=(low+high)/2;
if(element<arr[mid]){
high=mid-1;
}
else if(element>arr[mid]){
low=mid+1;
}
else if(element==arr[mid]){
printf("Element %d found at pos %d ",element,mid);
flag=1;
break;
}
}
if(flag==0){
printf("Element not found");
}
return 0;
}
The problem is your while test. You have:
while(low<high) {
...
}
This will fail when low == high if the desired value is at that position. It is easily fixed by changing the test to:
while(low <= high) {
...
}
This is all that's needed to fix it. You don't need to add any special cases to "fix it up". Just make sure your array is in ascending order and it should work.
EDIT: Refer to the better answer by #TomKarzes
My old answer is:
You missed a boundary case of high==low
#include<stdio.h>
int main(){
int size;
printf("Enter the number of elements(odd number) : ");
scanf("%d",&size);
int arr[size];
printf("Enter the elements in ascending order : ");
for(int i=0;i<size;i++){
scanf("%d",&arr[i]);
}
int element;
int flag=0;
printf("Enter element to be found : ");
scanf("%d",&element);
int low=0;
int high=size-1;
while(low<high){
int mid=(low+high)/2;
if(element<arr[mid]){
high=mid-1;
}
else if(element>arr[mid]){
low=mid+1;
}
else if(element==arr[mid]){
printf("Element %d found at pos %d ",element,mid);
flag=1;
break;
}
}
if(low==high && arr[low]==element) //Added 1 extra condition check that you missed
{
printf("Element %d found at pos %d ",element,low);
flag=1;
}
if(flag==0){
printf("Element not found");
}
return 0;
}
For starters for the number of elements of the array you shell use the type size_t. An object of the type int can be small to accommodate the number of elements in an array.
This condition of the loop
int high=size-1;
while(low<high){
//...
is incorrect. For example let's assume that the array has only one element. In this case high will be equal to 0 and hence equal to left due to its initialization
int high=size-1;
So the the loop will not iterate and you will get that the entered number is not found in the array though the first and single element fo the array actually will be equal to the number.
You need change the condition like
while ( !( high < low ) )
//...
This if statement within the else statement
else if(element==arr[mid]){
is redundant. You could just write
else // if(element==arr[mid]){
It would be better if the code that performs the binary search will be placed in a separate function.
Here is a demonstrative program that shows how such a function can be written.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int binary_search( const int a[], size_t n, int value )
{
size_t left = 0, right = n;
int found = 0;
while ( !found && left != right )
{
size_t middle = left + ( right - left ) / 2;
if ( value < a[middle] )
{
right = middle;
}
else if ( a[middle] < value )
{
left = middle + 1;
}
else
{
found = 1;
}
}
return found;
}
int cmp( const void *a, const void *b )
{
int left = *( const int * )a;
int right = *( const int * )b;
return ( right < left ) - ( left < right );
}
int main(void)
{
const size_t N = 15;
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
size_t n = rand() % N + 1;
int a[n];
for ( size_t j = 0; j < n; j++ ) a[j] = rand() % N;
qsort( a, n, sizeof( int ), cmp );
for ( size_t j = 0; j < n; j++ )
{
printf( "%d ", a[j] );
}
putchar( '\n' );
int value = rand() % N;
printf( "The value %d is %sfound in the array\n",
value, binary_search( a, n, value ) == 1 ? "" : "not " );
}
return 0;
}
Its output might look for example the following way
0 2 2 3 4 5 7 7 8 9 10 12 13 13
The value 5 is found in the array
4 8 12
The value 10 is not found in the array
1 2 6 8 8 8 9 9 9 12 12 13
The value 10 is not found in the array
2 3 5 5 7 7 7 9 10 14
The value 11 is not found in the array
0 1 1 5 6 10 11 13 13 13
The value 7 is not found in the array
0 3 3 3 4 8 8 10 11 12 14 14 14 14
The value 3 is found in the array
0 5 5 10 11 11 12 13 13 14 14
The value 12 is found in the array
3 4 5 7 10 13 14 14 14
The value 14 is found in the array
0 3 3 7
The value 2 is not found in the array
1 6 9
The value 10 is not found in the array
2 2 3 3 4 4 4 5 5 6 8 8 9 13 13
The value 11 is not found in the array
11 11 13
The value 11 is found in the array
0 0 0 1 2 5 5 5 7 7 8 9 12 12 14
The value 6 is not found in the array
8 8 13
The value 1 is not found in the array
2 2 4 4 5 9 9 10 12 12 13 13 14 14
The value 14 is found in the array
I have an array called int arr[10] = {1,2,3,4,5}
From my understanding the rest of the array is filled with 0's.
My questions is if its a fixed array length how can I put the first index behind the last index that is not a 0. For example
I believe the 0 is not shown in real printf but I am including it for illustration purposes
for (int i = 0 ; i < 10 ; i++)
{
print("%i" , arr[i]);
}
The output
1 2 3 4 5 0 0 0 0 0
If i move the first index to the back of the 5 like so
for (int i = -1 ; i < 10 ; i++)
{
arr[i] = arr[i + 1];
print("%i" , arr[i]);
}
Will the output put the 1 behind the 5 or at the back of the whole array?
2 3 4 5 1 0 0 0 0 0
or because there is 0s then
2 3 4 5 0 0 0 0 0 1
If my question is unclear please tell me and I will try explain it.
The output
1 2 3 4 5 0 0 0 0 0
No, the actual output is
1234500000
Your code has undefined behavior. The first iteration of the loop (with i = -1) tries to assign to arr[-1], which does not exist:
arr[i] = arr[i + 1];
Similarly, the last iteration (with i = 9) tries to read from arr[10], which also does not exist.
I'm not sure why you think your code will move the first element back.
From my understanding the rest of the array is filled with 0's
You are right.:)
If i move the first index to the back of the 5 like so
for (int i = -1 ; i < 10 ; i++)
{
arr[i] = arr[i + 1];
print("%i" , arr[i]);
}
then you will get undefined behavior because the indices -1 and 10 are not valid indices.
It seems what you are trying to do is the following
#include <stdio.h>
#include <string.h>
int main(void)
{
enum { N = 10 };
int a[N] = { 1, 2, 3, 4, 5 };
size_t pos = 0;
while ( pos < N && a[pos] != 0 ) ++pos;
if ( pos != N && !( pos < 3 ) )
{
int tmp = a[0];
pos -= 2;
memmove( a, a + 1, pos * sizeof( int ) );
a[pos] = tmp;
}
for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
putchar( '\n' );
return 0;
}
The program output is
2 3 4 1 5 0 0 0 0 0
I need to iterate over an array of n elements (dynamic size thus), for example 4, as if it were a binary number, starting with all zeros ({0,0,0,0}). Every time this needs to increment as if it is binary, {0,0,0,0} -> {0,0,0,1} -> {0,0,1,0} -> {0,0,1,1} -> {0,1,0,0}, etc...
I have trouble generating an algorithm that does so with array values, I thought about using recursion but cannot find the method to do so other than hard-coding in ifs. I suppose I could generate an n-digit number and then apply any algorithm discussed in this post, but that would be inelegant; the OP asked to print n-digit numbers while I need to work with arrays.
It would be great if someone could point me in the right direction.
Context:
int n;
scans("%d", &n);
int *arr = calloc(n, sizeof(int));
Algorithm:
Start at the rightmost element.
If it's a zero, then set it to one and exit
It must be a one; set it to zero
If you are at the left-most element, then exit.
You aren't already at the left-most element; move left one element and repeat.
I'm sorry, I'm not in a position to provide a code sample.
Algorithm:
Start at the rightmost element.
If it's a zero, then set it to one and exit
It must be a one; set it to zero
If you are at the left-most element, then exit.
You aren't already at the left-most element; move left one element and repeat.
Here's Hymie's algorithm, I'll try making a code out of it :
#include <stdlib.h>
#include <stdio.h>
int my_algo(int *arr, int size) {
for (int i = size - 1; i >= 0; i--) { // Start at the rightmost element
if (arr[i] == 0) { // If it's a zero, then set it to one and exit
arr[i] = 1;
return 1;
} else if (arr[i] == 1) { // If it's a one, set it to zero and continue
arr[i] = 0;
}
}
return 0; // stop the algo if you're at the left-most element
}
int main() {
int n;
scanf("%d", &n);
int *arr = calloc(n, sizeof(int));
do {
for (int i = 0; i < n; i++) {
putchar(arr[i] + '0');
}
putchar('\n');
} while (my_algo(arr, n));
return (0);
}
This algorithm is dynamic and work with scanf.
Here's the result for 4 :
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
What you seem to want to do is implement binary addition (or more precisely a binary counter), which is anyway implemented in the CPU's digital circuit using logic gates. It can be done using combination of logical operations (nand and xor to be exact).
But the most elegant approach according to my sense of elegance would be to use the CPU's innate ability to increment numbers and write a function to decompose a number to a bit array:
void generate_bit_array(unsigned int value, uint8_t *bit_array, unsigned int bit_count) {
for (unsigned int i=0; i<bit_count; i++) {
bit_array[i] = value >> (bit_count - i - 1) & 0x01;
}
}
int main(int argc, void **argv) {
unsigned int i;
uint8_t my_array[4];
/* Iterate and regenerate array's content */
for (i=0; i<4; i++) {
generate_bit_array(i, my_array, 4);
/* Do something */
}
return 0;
}
You can do:
#include <stdio.h>
#include <stdlib.h>
void gen_bit_array(int *numarr, int n, int arr_size) {
if(n > 0) {
numarr[arr_size-n] = 0;
gen_bit_array(numarr, n-1, arr_size);
numarr[arr_size-n] = 1;
gen_bit_array(numarr, n-1, arr_size);
} else {
int i;
for(i=0; i<arr_size; i++)
printf("%d", numarr[i]);
printf ("\n");
}
}
int main() {
int n,i;
printf ("Enter array size:\n");
scanf("%d", &n);
int *numarr = calloc(n, sizeof(int));
if (numarr == NULL)
return -1;
gen_bit_array(numarr, n, n);
return 0;
}
Output:
Enter array size:
2
00
01
10
11
Enter array size:
4
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
If I have understood you correctly then what you need is something like the following
#include <stdio.h>
#define N 4
void next_value( unsigned int a[], size_t n )
{
unsigned int overflow = 1;
for ( size_t i = n; i != 0 && overflow; i-- )
{
overflow = ( a[i-1] ^= overflow ) == 0;
}
}
int empty( const unsigned int a[], size_t n )
{
while ( n && a[n-1] == 0 ) --n;
return n == 0;
}
int main(void)
{
unsigned int a[N] = { 0 };
do
{
for ( size_t i = 0; i < N; i++ ) printf( "%i ", a[i] );
putchar( '\n' );
next_value( a, N );
} while ( !empty( a, N ) );
return 0;
}
The program output is
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
Or you can write the function that evaluates a next value such a way that if the next value is equal to 0 then the function returns 0.
For example
#include <stdio.h>
#include <stdlib.h>
int next_value( unsigned int a[], size_t n )
{
unsigned int overflow = 1;
for ( ; n != 0 && overflow; n-- )
{
overflow = ( a[n-1] ^= overflow ) == 0;
}
return overflow == 0;
}
int main(void)
{
size_t n;
printf( "Enter the length of the binary number: " );
if ( scanf( "%zu", &n ) != 1 ) n = 0;
unsigned int *a = calloc( n, sizeof( unsigned int ) );
do
{
for ( size_t i = 0; i < n; i++ ) printf( "%i ", a[i] );
putchar( '\n' );
} while ( next_value( a, n ) );
free( a );
return 0;
}
The program output might look like
Enter the length of the binary number: 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
No need for an algorithm, you could build a function,
Here is a process which could be a good idea if the array is of a fixed size :
Build a function which take your array
Create a local integrer for this function
Set the bits value of the integrer to the cells value of the array
Increment the integrer
Set the array values to match each bits of the integrer
All this process can be done using shifts (>> <<) and masks (& 255 for example).
I have to read in a text file with names and numbers. The names represent candidates in a dummy election (7 in total) and the numbers represent the voters. If the voter number is not in the range of the 7 candidates it gets thrown out but still stored. Finally, I have to print out the results of who won the election and how many spoilt votes there were.
This is my text file:
Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White
3 8 1 3 1 6 12 9 6 5 0 2 8 4
6 6 8 3 2 8 0 12 6 1 8 3 2 2
3 2 5 7 4 11 8 6 11 12 11 7 5 5
8 9 10 12 1 3 12 12 9 11 7 9 3 1
2 10 12 7 11 9 6 6 0 1 10 7 11 2
8 0 12 8 10 11 2 2 8 4 2 12 3 2
9 1 4 8 8 7 7 4 12 2 10 10 9 4
12 9 3 12 0 4 8 0 6 5 9 0 5 3
11 6 0 3 0
This is where I am stuck about how to scan these in properly
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
FILE * data;
int spoilt=0;
typedef struct
{
int votes;
char name[20];
}candidates;
void initialize( candidates *electionCandidates, FILE *data )
{
int i;
for( i=0; i<7; i++ )
{
fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
printf( "%s\n", electionCandidates[i].name );
electionCandidates[i].votes=0;
}
}
int processVotes( candidates *electionCandidates, FILE *data )
{
int i; //tallying votes
int voter;
for ( i = 0; i< 365; i++ )
{
fscanf( data, "%d", voter );
if ( voter <= 7&& voter > 0 )
electionCandidates[voter-1].votes++;
else
spoilt++;
}
//catcher to grab winner
int maxValue, winner=0;
maxValue = electionCandidates[0].votes;
for( i = 0; i < 7; i++ )
{
if( maxValue < electionCandidates[i].votes )
{
maxValue = electionCandidates[i].votes;
electionCandidates[winner] = electionCandidates[i];
}
}
return electionCandidates[winner], maxValue;
}
void printResults( candidates *electionCandidates )
{
printf("%s won the election with a total of %d votes.\n There was a total of %d spoilt"
electionCandidates[winner].name, maxValue, spoilt);
}
int main() {
data = fopen( "elections.txt","r" );
candidates electionCandidates[7];
initialize( electionCandidates, data );
processVotes( electionCandidates, data );
printResults( electionCandidates );
fclose( data );
return 0;
}
When using scanf, you must provide the address of the variable that you want to scan the result into. Provide the address by using the & operator. Also, it is a good idea to check the result of scanf to ensure that it successfully scanned what you asked for. scanf will always return the number of elements successfully scanned, unless an I/O error occurred, in which case it will return a negative number.
Here's an fixed, annotated version of your program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
int votes;
char name[20];
}candidates;
// specify a new type to hold the election result data
typedef struct
{
int winner;
int maxVotes;
int spoilt;
} electionResult;
void initialize( candidates *electionCandidates, FILE *data )
{
int i;
for( i=0; i<7; i++ )
{
fscanf( data, "%[^\n]%*c", electionCandidates[i].name );
printf( "%s\n", electionCandidates[i].name );
electionCandidates[i].votes=0;
}
}
// This function can now return more than one value, because we've wrapped
// the relevant info into a structure called "electionResult"
electionResult processVotes( candidates *electionCandidates, FILE *data )
{
// declare the election result struct here (which we fill with data)
// we initially set all values to 0
electionResult er = {0, 0, 0};
int i; //tallying votes
int voter;
for ( i = 0; i< 365; i++ )
{
// scan the vote by providing the address of voter (using &)
int result = fscanf( data, "%d", &voter );
if (result == 1)
{
if ( voter <= 7&& voter > 0 )
electionCandidates[voter-1].votes++;
else
er.spoilt++;
}
}
er.maxVotes = electionCandidates[0].votes;
for( i = 0; i < 7; i++ )
{
if( er.maxVotes < electionCandidates[i].votes )
{
// update the values in the election result struct
er.maxVotes = electionCandidates[i].votes;
er.winner = i;
}
}
return er;
}
// this function now prints the result of the election by accepting an "electionResult" struct
void printResults( candidates *electionCandidates, electionResult er )
{
printf("%s won the election with a total of %d votes.\n There was a total of %d spoilt",
electionCandidates[er.winner].name, er.maxVotes, er.spoilt);
}
int main() {
FILE *data = fopen( "elections.txt","r" );
candidates electionCandidates[7];
electionResult er;
initialize( electionCandidates, data );
er = processVotes( electionCandidates, data );
printResults( electionCandidates, er );
fclose( data );
return 0;
}
Some tips:
You can't access variables declared in other functions. You must return the data you want from one function and provide it to the other function.
Avoid having variables declared at file scope if you can. For simple programs like this, it isn't much of an issue, but in general, using global variables tends to get messy fast.
You can't return more than one value from a function unless you wrap up the values in a struct, like the above, or alternatively, have your function accept pointers to the objects that will hold the result, similar to how fscanf accepts &voter and subsequently fills the voter variable with the appropriate data (if it can).