Define an array and the type of a function in C

Asked

Viewed 215 times

2

Hello,

I am developing a C language boot project, namely the creation of a Multimedia Management System that will have to be able to manage a database of discographic articles such as Cds, Dvds and Vinyl Discs.

At the moment, after declaring the structure (I do not know if it is correct), I have this doubt:

  • How to define an array that can contain articles and then define the function type consultation() and if so, how to define this function? Basically, I wanted to "tell" the function what the reference of the article so that the function can find me this book. In the end, the function would have to return me the result of the article.

Here’s my source code for now:

#include <stdio.h>
#include <stdlib.h>

// prototipagem de funções:
int menuPrincipal(void);
void consultarArtigo(void);
void removerArtigo(void);
void alterarArtigo(void);
void inserirArtigo(void);

// estruturas:
typedef struct {
    int numero_unico_de_registo;
    char titulo_do_artigo[30];
    char area_discografica; // dizer se é do tipo CD, DVD ou Vinil?
    char nome_do_artista[30];
    int ano_de_lancamento;
    int estante_de_localizacao; // as estantes seriam identificadas por numeros?    
} ARTIGO;

// Função MAIN:
int main()
{
    menuPrincipal();
    return;
}

// Funções AUX:
int menuPrincipal(void){

    int opcao;

    do {

    printf("Bem-Vindo ao MMS!\n");
    printf("1 - Consultar Artigo Discografico\n");
    printf("2 - Remover Artigo Discografico\n");
    printf("3 - Alterar Artigo Discografico!\n");
    printf("4 - Inserir Artigo!\n");
    printf("0 - Fechar!\n");
    printf("Escolha a sua opcao!\n");
    scanf("%d", &opcao);

    switch(opcao) {
        case 1: consultarArtigo();
        break;
        case 2: removerArtigo();
        break;
        case 3: alterarArtigo();
        break;
        case 4: inserirArtigo();
        break;
        case 0: printf("Sair do Menu!");
        default: printf("Opcao invalida, tente de novo.\n");
    }

    } while (opcao != 0);

    return(opcao);
}

void consultarArtigo(void) {
    printf("Entrei na funcao Consultar Artigo!\n");
}

void removerArtigo(void) {
    printf("Entrei na funcao Remover Artigo!\n");
}

void alterarArtigo(void){
    printf("Entrei na funcao Alterar Artigo!\n");
}

void inserirArtigo(void){
    printf("Entrei na funcao Inserir Artigo!\n");
}

Code originally placed on ideone

Any hint would be welcome.

1 answer

2

1) What is the data?

The first part is to define a structure that will store the articles and all the information about it you need, which you have already done.

// estruturas:
typedef struct {
    int numero_unico_de_registo;
    char titulo_do_artigo[30];
    char area_discografica; // dizer se é do tipo CD, DVD ou Vinil?
    char nome_do_artista[30];
    int ano_de_lancamento;
    int estante_de_localizacao; // as estantes seriam identificadas por numeros?    
} ARTIGO;

2) What is the structure?

The second part is to define which data structure you will use. If the number of active articles has a limit (maybe the number you imagine as the maximum for your library), there is not much problem you use an array, but it is possible that you need to allocate indefinitely memory to place the articles, in that case I recommend you try to use a tree or a list.

You have to decide whether to keep the articles sorted by the registration number or not. From what I understand, you’ll need to search for any field, so it doesn’t seem to help you much to sort.

To declare the array, just, just below the definition of the ARTICLE, put:

#define NUMERO_MAXIMO_DE_ARTIGOS 50 //número máximo permitido
struct listaDeArtigos ARTIGO[NUMERO_MAXIMO_DE_ARTIGOS]; //declaração do array
int numeroDeArtigos = 0; //para guardar o numero atual de artigos

3) Implementing the functions

Here are some tips:

To make the query, just ask the user which field he wants to search and the value of the field. Then you go through all the positions up to the numberDerticles and returns the found article or returns not found.

When removing an article, move all others to the end, always keeping the beginning of your array filled.

Always try to insert an article at the end if the order makes no difference.

The article change function is basically a progress of the query function, so make it first.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.