Jump to content
// I have found that it works best for my first program to be a madlib generator

#include <stdio.h>
#include <stdlib.h>
#include <time.h> // Needed for time()

int main() {
    // Seed the random number generator
    srand(time(NULL));
   
    /* 
     * The words to use for the mad lib
     * -for some reason, it didn't like the num array being a pointer
     * -the rest had to be pointers in order for me to reference them
    */ 
    char *adj[] = {"Dirty", "Ugly", "Big", "Small", "Stinky", "Shiny", "Happy", "Sad", "Bright", "Dark", "Quiet", "Loud", "Strong", "Weak", "Clever"};
    char *verb[] = {"Run", "Jump", "Swim", "Smell", "Look", "Sleep", "Sail", "Dig", "Fly", "Think", "Eat", "Sing", "Dance", "Read"};
    char *noun[] = {"Apple", "Boot", "Boy", "Girl", "Watch", "Phone", "Cat", "Dog", "House", "Car", "Tree", "Book", "Computer", "River", "Mountain"};
    int num[] = {1, 2, 45, 78, 80, 99, 7, 5, 10, 25, 50, 75, 100, 200, 500};
    char *color[] = {"Red", "Blue", "Green", "Yellow", "Orange", "Purple", "Pink", "Black", "White", "Gray", "Brown", "Cyan", "Magenta", "Teal", "Lime"};
    
    // Random selections
    // adjectives
    char *adj1 = adj[rand() % 15];
    char *adj2 = adj[rand() % 15];
    char *adj3 = adj[rand() % 15];
    //verbs
    char *verb1 = verb[rand() % 15];
    char *verb2 = verb[rand() % 15];
    char *verb3 = verb[rand() % 15];
    // nouns
    char *noun1 = noun[rand() % 15];
    char *noun2 = noun[rand() % 15];
    char *noun3 = noun[rand() % 15];
    // colors
    char *color1 = color[rand() % 15];
    char *color2 = color[rand() % 15];
    char *color3 = color[rand() % 15];
    //num
    int num1 = num[rand() % 15];   
    int num2 = num[rand() % 15];  
    int num3 = num[rand() % 15];   
    
    // determines what story template to use
    int storyNum = rand() & 2; 
    switch (storyNum) {
        case 0:
            printf("One %s morning, a %s %s decided to %s a %s %s.\n", adj1, adj2, noun1, verb1, color1, adj3);
            break;
        case 1:
            printf("After %d long hours, the %s %s finally managed to %s the %s %s.\n", num1, adj1, noun1, verb1, color1, adj2);
            break;
        case 2:
            printf("In a %s land, the %s and %s %s tried to %s a mysterious %s treasure guarded by a %d-headed dragon.\n", color1, adj1, adj2, noun1, verb1, adj3, num1);
            break;
    }
    return 0;
}

Lately I have been experimenting with programming in C, and this is my first (real) program. Was posting to get some thoughts or suggestions about C/ this program.

 

For context the first language I learned was JavaScript but wanted to learn a more performant language for making UNIX programs so obviously I looked into C. I enjoy the simplicity of it, and it’s forcing me to lean low level concepts. I don’t program as a job, nor plan too, it’s just a hobby.

My Main rig: i7-4790, Asus z97c, Radeon HD 7850 (2gig), Fractal Design Focus G.

My Laptop: Dell Latitude e5410, Tiny 11, 8gb ram, i5-450m 120 gig SSD.

Beware Yankees I'm a Louisianan  *Rebel Yell*

 

 

 

 

 

Link to comment
https://linustechtips.com/topic/1610824-tips-on-c-programmingrate-my-first-program/
Share on other sites

Link to post
Share on other sites

It's cool 🙂

You could easily improve to non fixed sized arrays and externalize the "dictionary" as a data file to make it "evolutive"

AMD R9  7950X3D CPU/ Asus ROG STRIX X670E-E board/ 2x32GB G-Skill Trident Z Neo 6000CL30 RAM ASUS TUF Gaming AMD Radeon RX 7900 XTX OC Edition GPU/ Phanteks P600S case /  Arctic Liquid Freezer III 360 ARGB cooler/  2TB WD SN850 NVme + 2TB Crucial T500  NVme  + 4TB Toshiba X300 HDD / Corsair RM850x PSU/ Alienware AW3420DW 34" 120Hz 3440x1440p monitor / ASUS ROG AZOTH keyboard/ Logitech G PRO X Superlight mouse / Audeze Maxwell headphones

Link to post
Share on other sites

7 minutes ago, PDifolco said:

It's cool 🙂

You could easily improve to non fixed sized arrays and externalize the "dictionary" as a data file to make it "evolutive"

Thank you!, what do you mean by making it evolutive?

My Main rig: i7-4790, Asus z97c, Radeon HD 7850 (2gig), Fractal Design Focus G.

My Laptop: Dell Latitude e5410, Tiny 11, 8gb ram, i5-450m 120 gig SSD.

Beware Yankees I'm a Louisianan  *Rebel Yell*

 

 

 

 

 

Link to post
Share on other sites

Being able to change the words etc without recompiling the program.

 

29 minutes ago, Drvulcanlord said:
/* 
     * The words to use for the mad lib
     * -for some reason, it didn't like the num array being a pointer
     * -the rest had to be pointers in order for me to reference them
    */ 

Each string is an array of chars, so when you define one you get a pointer to the first char in the string, so your array needs to be an array of pointers to chars. The ints are just ints directly.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to post
Share on other sites

7 minutes ago, Drvulcanlord said:

Thank you!, what do you mean by making it evolutive?

Being able to change or add words, not only a fixed set which needs you to change the code for that

AMD R9  7950X3D CPU/ Asus ROG STRIX X670E-E board/ 2x32GB G-Skill Trident Z Neo 6000CL30 RAM ASUS TUF Gaming AMD Radeon RX 7900 XTX OC Edition GPU/ Phanteks P600S case /  Arctic Liquid Freezer III 360 ARGB cooler/  2TB WD SN850 NVme + 2TB Crucial T500  NVme  + 4TB Toshiba X300 HDD / Corsair RM850x PSU/ Alienware AW3420DW 34" 120Hz 3440x1440p monitor / ASUS ROG AZOTH keyboard/ Logitech G PRO X Superlight mouse / Audeze Maxwell headphones

Link to post
Share on other sites

C is a very good language to learn how computer works. It is barely just a step above assembly. Since you plan on creating softwares for UNIX, check out the POSIX Application Programming Interfaces. These can teach you how an operating system works under the hood and also help your software to be very portable on these UNIX and UNIX like platforms. Although to be honest, if you only stick to the C standard library, pretty sure you can port and cross compile on just any of the popular operating systems and platforms. 

Sudo make me a sandwich 

Link to post
Share on other sites

27 minutes ago, wasab said:

C is a very good language to learn how computer works. It is barely just a step above assembly. Since you plan on creating softwares for UNIX, check out the POSIX Application Programming Interfaces. These can teach you how an operating system works under the hood and also help your software to be very portable on these UNIX and UNIX like platforms. Although to be honest, if you only stick to the C standard library, pretty sure you can port and cross compile on just any of the popular operating systems and platforms. 

Thanks, I’ll do that!

My Main rig: i7-4790, Asus z97c, Radeon HD 7850 (2gig), Fractal Design Focus G.

My Laptop: Dell Latitude e5410, Tiny 11, 8gb ram, i5-450m 120 gig SSD.

Beware Yankees I'm a Louisianan  *Rebel Yell*

 

 

 

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×