• 0

I am gonna explode -> easy C program


Question

Here is the input http://pastebin.com/BBHxd0iY. I have to evaluate in each line what is the county with the most people and from all the tests to that line the parish with the least people from that county. I am using a linked list. The county with the most people should be the root. I really need a fast reply xD.

District, county, parish, population.

pessoa = people, concelho = county, numero = number, nome = name, freguesia = parish, insere = insert, pesquisa = search, proximo = next.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lista.h"

concelho* criarConcelho( char *nomeConcelho, int pessoas ) {

concelho *ptr;

if ( ( ptr = ( concelho *) malloc( sizeof(concelho) * 1 ) ) == NULL ) return NULL;

ptr->concelho = nomeConcelho;
ptr->numeroPessoas = pessoas;
ptr->proximo = NULL;

return ptr;
}

freguesia* criarFreguesia ( char *nomeFreg, int populacao ) {

freguesia *ptr;

if ( ( ptr = ( freguesia *) malloc( sizeof(freguesia) * 1 ) ) == NULL ) return NULL;

ptr->freguesia = nomeFreg;
ptr->populacao = populacao;

return ptr;
}

FILE* readlnVeriPlace( FILE *input, baseConcelhos *bConcelhos) {

int i;
int numeroPessoas;
concelho *ptr, *intelliScan;
freguesia *freg;
char *concelho;
char *freguesia;
char ch;
int pos;


if ( input == NULL || bConcelhos == NULL ) return NULL;

freguesia = (char *) malloc ( sizeof(char) * 50 );
concelho = (char *) malloc ( sizeof(char) * 50 );
if ( freguesia == NULL || concelho == NULL ) return input;

// READLINE

while ( ( ch = fgetc(input) ) != ',' );

i = 0;
while ( ( ch = fgetc(input) ) != ',' ) {
concelho[i] = ch;
i++;
}
concelho[i] = '\0';

i = 0;
while ( ( ch = fgetc(input) ) != ',' ) {
freguesia[i] = ch;
i++;
}
freguesia[i] = '\0';

fscanf(input, "%d", &numeroPessoas);

while ( fgetc(input) != '\n' && !feof(input)) ;

//ENDREADLINE

intelliScan = lista_pesquisa( bConcelhos, concelho, &pos);

if ( intelliScan != NULL ) {
lista_atribui( bConcelhos, pos, &freguesia, numeroPessoas);
free(concelho);
}
else {
freg = criarFreguesia ( freguesia, numeroPessoas );
ptr = criarConcelho ( concelho, numeroPessoas );
ptr->freg = freg;
intelliScan = ptr;
if ( bConcelhos->numero == 0 ) lista_insere( bConcelhos, -1, ptr);
}

lista_ordena( bConcelhos, pos, intelliScan);

return input;
}


int main( int argc, const char *argv[] ) {

FILE *input;
FILE *output;
baseConcelhos *base;
concelho *umDoLiTa;

if ( argc != 2 ) {
fprintf( stdout, "%s shuffle.csv\n", argv[0] );
exit(1);
}

if ( ( input = fopen( argv[1], "r") ) == NULL ) {
fprintf( stdout, "Not possible to open the file.\n");
exit(2);
}

if ( ( output = fopen ( "output.csv", "w") ) == NULL ) exit(3);

if ( ( base = (baseConcelhos *) malloc( sizeof(baseConcelhos) * 1) ) == NULL ) exit (4);

base->numero = 0;
base->proximo = NULL;

while ( !feof(input) ) {
input = readlnVeriPlace( input, base );
fprintf( stdout, "%s, %s, %d\n", base->proximo->freg->freguesia, base->proximo->concelho, base->proximo->freg->populacao);
}

return 0;
}[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1078461-i-am-gonna-explode-easy-c-program/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

typedef struct {
int populacao;
char *freguesia;
}freguesia;

typedef struct concelhos {
char *concelho;
int numeroPessoas;
freguesia *freg;
struct concelhos *proximo;
} concelho;

typedef struct baseConcelhos {
int numero;
concelho *proximo;
} baseConcelhos;

int lista_atribui( baseConcelhos *l, int pos, char **str, int numeroP);

int lista_insere( baseConcelhos *l, int pos, concelho *curr);

concelho* lista_trocaAUX(baseConcelhos *l, int pos);

concelho * lista_pesquisa( baseConcelhos *l, char* str, int *pos );

int lista_ordena( baseConcelhos *l, int posAlt, concelho *intelliScan );
[/CODE]

  • 0

#include "lista.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int lista_atribui( baseConcelhos *l, int pos, char **str, int numeroP)
{
int i = 0;
concelho *aux;

if (l == NULL || pos < 0)
return -1;

aux = l->proximo;

for (i = 0; i < pos && aux != NULL; i++)
aux = aux->proximo;

if (aux == NULL)
return -1;

aux->numeroPessoas += numeroP;

if ( numeroP < aux->freg->populacao ) {
aux->freg->populacao = numeroP;
free(aux->freg->freguesia);
aux->freg->freguesia = *str;
} else free(*str);

return pos;
}


int lista_insere( baseConcelhos *l, int pos, concelho *curr)
{
int i = 0;
concelho *temp;

if (l == NULL || pos < -1 || pos >= l->numero) return -1;

temp = l->proximo;

if (curr == NULL) return -1;

l->numero++;

if(pos == -1)
{
if (temp == NULL) l->proximo = curr;
else
{
while (temp->proximo != NULL) temp = temp->proximo;
temp->proximo = curr;
}

return l->numero-1;
}

if (pos == 0)
{
curr->proximo = temp;
l->proximo = curr;
return pos;
}

for (i = 0; i < pos-1 && temp != NULL; i++) temp = temp->proximo;

curr->proximo = temp->proximo;
temp->proximo = curr;

return pos;
}


concelho* lista_trocaAUX(baseConcelhos *l, int pos)
{
int i = 0;
concelho *prev, *curr;

if (l == NULL || pos < 0 || pos >= l->numero) return NULL;

curr = l->proximo;

l->numero--;

if(pos == 0)
{
l->proximo = curr->proximo;
curr->proximo = NULL;
return curr;
}

for( i = 0; i < pos && curr->proximo; i++)
{
prev = curr;
curr = curr->proximo;
}

prev->proximo = curr->proximo;

curr->proximo = NULL;
return curr;
}

concelho * lista_pesquisa( baseConcelhos *l, char* str, int *pos)
{

int i = 0;
concelho *aux;

if( l == NULL ) return NULL;

for (aux = l->proximo; aux != NULL; aux = aux->proximo, i++)
{
if ( strcmp( aux->concelho, str) == 0) {
*pos = i;
return aux;
}
}

*pos = -1;
return NULL;
}

int lista_ordena( baseConcelhos *l, int posAlt, concelho *intelliScan)
{
concelho *ptr;
int i = 0;

if ( l == NULL ) return -1;
if ( l->proximo == NULL || l->numero == 1) return 0;

if ( posAlt != -1 ) intelliScan = lista_trocaAUX( l, posAlt);

for ( ptr = l->proximo; ptr != NULL; ptr = ptr->proximo, i++ ) {
if ( ptr->numeroPessoas < intelliScan->numeroPessoas ) {
lista_insere( l, i, intelliScan);
}
}
lista_insere( l, -1, intelliScan);

return 0;
}[/CODE]

  • 0

Where exactly are you stuck? Its quite difficult to understand code not written in your language (now I know how hard it must be for non-english speakers).

If you want the county with the largest number of people at the root, then use a sorting algorithm to get the list in order.

To begin with use something simple like bubble sort, then once that works you can try implementing faster and more efficient (but more complicated) sorting algorithms.

  • 0

Please delete this topic I will create a new one with the proper documentation. And in English, since tiagosilva29 which is Portuguese did not even give it a go. And just ++ to his massive posts. I am not worried with the exams which are next door, it is all under control. I just cant spot the glitch... Some extra pair of eyes would be appreciated.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Audacious 4.6.1 by Razvan Serea Audacious is a lightweight, open-source audio player that emphasizes simplicity, performance, and sound quality. Designed for Linux, Windows, and macOS, it supports a wide range of audio formats, internet radio streaming, and playlist management. Users can customize the interface with Winamp-style skins or modern themes, making it flexible for different preferences. Audacious also includes an equalizer, advanced audio effects, and a plugin system for extending functionality. Its low resource usage makes it especially suitable for older computers or users who value efficiency without sacrificing playback quality. Audacious key features: High audio quality – delivers clean, gapless playback with minimal distortion. Wide format support – plays MP3, FLAC, Ogg Vorbis, AAC, WAV, WMA, and more. Internet radio streaming – supports Shoutcast, Icecast, and other online streams. Winamp skin support – classic, nostalgic look for users who prefer the old-school style. Modern GTK-based interface – clean, simple UI with a more modern feel. Customizable themes – change appearance through skins and themes. Advanced playlist management – organize, save, and edit playlists with ease. Equalizer – fine-tune audio output with a built-in graphical equalizer. Audio effects – built-in DSP options like crossfade, replay gain, and more. Plugin system – extend functionality with additional components. File metadata support – displays and organizes music based on tags. Drag-and-drop support – quickly add songs or playlists. Global hotkey support – control playback without switching windows. Bit-perfect output modes – bypass system mixers for pure audio output. ReplayGain support – normalizes track loudness automatically. Cue sheet support – play entire albums from a single audio file with .cue. MPRIS2 integration – integrates with Linux desktop environments for media controls. Advanced resampling options – adjust playback quality with different resampler settings. Gapless playback – seamless transition between tracks encoded properly. Crossfade plugin – blend one song into the next smoothly. Last.fm scrobbling plugin – track listening history online. Remote control support – control Audacious via command-line or scripts. Lyrics plugin – display song lyrics if available. Alarm / timer plugin – start or stop playback at set times. SOX resampler plugin – high-quality resampling for audiophiles. Spectrum analyzer / visualization plugins – visual feedback while playing music. Headphone crossfeed effect – simulates speaker listening for headphones. Customizable buffer size – tweak latency and playback smoothness. Audacious 4.6.1 changelog: Use XDG cache dir to store temporary files (#1817) Accept embedded lyrics in more cases (#1818) Bump .so and plugin ABI versions retrospectively (#1819) Include Georgian translation (#1820) Fix build on systems using musl instead of glibc (#1823) Download: Audacious 4.6.1 | 48.2 MB (Open Source) Download: Portable Audacious 4.6.1 | 69.8 MB View: Audacious Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I really wonder if this has to do with the built in VPN or "private DNS" of browsers that trip up legal requirements like cookie consent and Cloudflare (to avoid all the botnet attacks we get). And BTW some botnets still manage to get past Cloudflare, we are constantly having to tweak it to block malicious traffic that ultimately cause a DDoS.
    • CPPC states can also be messed around with in most UEFI settings but aren't as robust as the ones that the Windows Scheduler can provide! Make sure you look into what your motherboard also has before customizing for the Windows Scheduler.
  • Recent Achievements

    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
    • One Month Later
      agatameier earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      513
    2. 2
      +Edouard
      199
    3. 3
      PsYcHoKiLLa
      146
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!