C Program : Explanation for the program shared on 2/08/2023

in c-programming •  last year 

Welcome back,
I am here with a the explanation of the program shared yesterday. If you want to skip the explanation for now then visit the blog shared yesterday.

Let us look at the program ,

image.png

image.png

image.png

image.png

There are 'S' and 's' at few places in the above picture and they might have got written while i was taking the screenshot of the written program.

You can copy the code from below :

#include <stdio.h>
#include <string.h>

#define MAX_CRYPTO 5
#define MAX_NAME_LENGTH 10

void printCryptoArray(char crypto[MAX_CRYPTO][MAX_NAME_LENGTH], int numCrypto) {
printf("Current Cryptos: ");
for (int i = 0; i < numCrypto; i++) {
printf("%s ", crypto[i]);
}
printf("\n");
}

int main() {
char crypto[MAX_CRYPTO][MAX_NAME_LENGTH] = {"DOGE", "BTC", "HIVE", "ETH", "LTC"};
int numCrypto = 5;

while (1) {
char action[10];
printf("\nEnter 'add' to add a new crypto, 'remove' to remove a crypto, or 'exit' to quit: ");
scanf("%s", action);

if (strcmp(action, "add") == 0) {
if (numCrypto >= MAX_CRYPTO) {
printf("Maximum number of cryptos reached!\n");
continue;
}

char newCrypto[MAX_NAME_LENGTH];
printf("Enter the name of the new crypto: ");
scanf("%s", newCrypto);
// Check if the new crypto already exists
int cryptoExists = 0;
for (int i = 0; i < numCrypto; i++) {
if (strcmp(crypto[i], newCrypto) == 0) {
cryptoExists = 1;
break;
}
}

if (cryptoExists) {
printf("%s already exists in the list.\n", newCrypto);
} else {
strcpy(crypto[numCrypto], newCrypto);
numCrypto++;
printf("%s has been added to the list.\n", newCrypto);
}
} else if (strcmp(action, "remove") == 0) {
char removeCrypto[MAX_NAME_LENGTH];
printf("Enter the name of the crypto to remove: ");
scanf("%s", removeCrypto);

int foundIndex = -1;
for (int i = 0; i < numCrypto; i++) {
if (strcmp(crypto[i], removeCrypto) == 0) {
foundIndex = i;
break;
}
}

if (foundIndex == -1) {
printf("%s does not exist in the list.\n", removeCrypto);
} else {
for (int i = foundIndex; i < numCrypto - 1; i++) {
strcpy(crypto[i], crypto[i + 1]);
}
numCrypto--;
printf("%s has been removed from the list.\n", removeCrypto);
}
} else if (strcmp(action, "exit") == 0) {
printf("Exiting the program.\n");
break;
} else {
printf("Invalid action. Please try again.\n");
}

printCryptoArray(crypto, numCrypto);
}

return 0;
}

We start by including the necessary header files: stdio.h for input/output operations and string.h for string manipulation functions.

We define some constants to set the maximum number of cryptos and the maximum length of the crypto name.

The main function begins, and we declare an array crypto to store the crypto names. We also initialize a variable numCrypto to keep track of the number of cryptos currently in the array. We pre-fill the crypto array with five initial crypto names: DOGE, BTC, HIVE, ETH, and LTC.

Inside an infinite loop while (1), the program repeatedly asks the user for an action: "add", "remove", or "exit". This loop will keep running until the user chooses to exit.

The program prompts the user to enter their chosen action and reads it into the action array.

If the user selects "add", the program first checks if the array has reached its maximum capacity (MAX_CRYPTO). If it has, it prints a message and continues to the next iteration of the loop.

If not, the program proceeds to ask the user for the name of the new crypto they want to add (newCrypto).

It then checks if the new crypto name already exists in the crypto array. If it does, the program prints a message saying it's already present.

Otherwise, the new crypto is added to the array, and the numCrypto counter is incremented to keep track of the total number of cryptos.

If the user selects "remove", the program prompts the user to enter the name of the crypto they want to remove (removeCrypto).

It then searches for the crypto in the crypto array and stores its index if found (foundIndex).

If the crypto is not found, the program prints a message indicating that it doesn't exist.

Otherwise, the program shifts the elements to the left to remove the crypto from the array and decrements the numCrypto counter.

If the user selects "exit", the program prints a farewell message and breaks out of the infinite loop, effectively ending the program.

For any other invalid action, the program prints an error message and continues to the next iteration of the loop.

After performing the action, the program calls the printCryptoArray function to display the current list of cryptos.

The printCryptoArray function iterates through the crypto array and prints the names of all the cryptos that are currently in the array.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!