-2

I have these challenges:

  1. Define a struct containing 4 data types
  2. Write a program in C that uses the rand() function to create 1000 instances of these structs
  3. Inserts them in the linked list and prints out the first 10 to the console.

Modify the linked list code provided in the file code.c so that it works for inserting structs of type given in your answer to Question 1.

#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
//#include "users.h"

int i;
//Stime = srand(time(0));

typedef struct users
{

    int UserID;
    char FullName[50];
    int Age;
    //double Height;
    float Weight;
}users;


typedef struct node
{
    //Stime = srand(time(0));
    users data;
    struct node *next;
} node;


node* insert(node *ptr, users data)
{

    node *entry = (node*)malloc(sizeof(node));
    //printf("enter the data item\n");
    //scanf("%d",*node-> next);
    if(entry == NULL)
    {
        printf("No Free Memory!\n");
        exit(0);
    }
    else
    {
        entry->data = data;
        entry->next = NULL;

        if(ptr == NULL)
        {
            ptr = entry;
        }
        else
        {
            node *temp = ptr;
            while(temp->next != NULL)
            {
                temp = temp->next;
            }

            temp->next = entry;
        }
     }
     return ptr;
}

/

obviously U[i] is not the correct way to do this. if I wanted to have unique constructors o-n (max 1000) how would I do it?

int main()
{
    int i= 0;
    node *first = NULL;
    srand(time(0));


 users U[i] = {  
    (U[i].UserID = 600000+rand()% 33331),
    (strcpy( U[i].FullName , "  Nathanial Rivers")),
    (U[i].Age = 18+rand()% 82),
    (U[i].Weight = 40+rand()% 99)
    };
    //users U1 = {600000,"Martin Toomey",19,76.6};
    users U2 = {(U2.UserID = 600000+rand()% 33331),"bob boby",21,77.7};
    users U3 = {600002,"abcdefg ",17,79.1};
    printf(" Name: %s  \n",U1.FullName);
    printf(" User ID: %d \n Age is: %d \n Weight is: %f \n \n",U1.UserID, U1.Age, U1.Weight );
    for (i=0; i<10; i++){
            srand(time(0));
            first = insert(first, U[i]);
            /first = insert(first, U2);
            //first = insert(first, U3);
    //printf(" User ID: %d \n Age is: %d \n Weight is: %f \n \n",U1.UserID, U1.Age, U1.Weight );}
        printf(" User ID: %d \n", U[i].UserID);
        printf(" Age %d \n", U[i].Age);
        printf(" User ID: %d \n", U[i].UserID);
        printf(" Age %d \n", U2.Age);
}
    //printf(U1);
    first = insert(first, U2);
    //printf(*U2);
    first = insert(first, U3);

    return 0;
}

In main() function, I'm trying to generate 100 unique users using rand I have thought that if I have a print function or param in first and every time I call first in the loop in prints the user info with C I'm not sure if that possible

Any pointers on how to improve my code greatly appreciated I know U1 U2 U3.... shouldn't be used as var names its bad practice.

Sebastian Roth
  • 11,344
  • 14
  • 61
  • 110
Toomey86
  • 1
  • 1
  • 1
    C and C# are two very different languages – Gilad Green Apr 07 '18 at 17:39
  • 1
    Also C and C++ are different too. `new` is a C++ keyword. Also `users` should be singular. – Phil1970 Apr 07 '18 at 18:00
  • thanks, I didn't know I thought it was just C and C++ – Toomey86 Apr 07 '18 at 18:00
  • I'm learning a lot of languages at the same time java, python C# & SQL so i doget a bit confused – Toomey86 Apr 07 '18 at 18:03
  • 1
    So, which language do you think you're writing this code in? It seems to be schizophrenic between C (most of it) and C++ (using `new`) but in a context with a VLA (variable length arrays) which are not a part of standard C++. The allocating code leaks memory horribly, and writes out of bounds, and generally doesn't look very sensible. Learning four languages at once is hard enough — coding in a fifth (and possibly sixth) complicates things still further. I don't envy you. You need to be sure you know which language you're intending to write in and make sure you use only that language. – Jonathan Leffler Apr 07 '18 at 18:17

1 Answers1

1

There are many problems in your posted code:

  1. struct users[] user[i] = new users [100]:

    • Semicolon ; missing at end of the line
    • by using a typedef you don't have to specify the type via struct
    • if you want to use an array you should create it above the for-loop
    • in C you allocate dynamic memory with malloc: user* tmp = malloc(sizeof(user))
  2. printf(user):

    • Semicolon ; missing at end of line
    • printf doesn't know the internal structure of the struct. So you have ot print have to declare it like with: printf(" User ID: %d \n Age is: %d \n Weight is: %f \n",Nathan.UserID, Nathan.Age, Nathan.Weight );
  3. struct users Nathan;

    • by using a typedef you don't have to specify the type via struct. Use user Nathan; instead

I would recommend you to use online resources or books and start with easier challenges.

Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74