Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
What should I look into to figure out how to transform two 1D arrays into a 2D array in C?

Adbot
ADBOT LOVES YOU

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.

Jeffrey of YOSPOS posted:

Definitely clarify more than that - do you want to end up with an n by 2 array? Maybe say what domain this is for? How is the 2d array going to be used? I kinda feel like that's the sort of thing that you ought to figure out how to piece together on your own, it's trivial if you know how things work and dangerous if you don't. Any reason not to loop through and copying wouldn't work.

Sorry, I'm still learning/self teaching C. Your questions helped me find a mistake.

I was using
code:
int locationXY[userinput][userinput]
both elements(?) of which could go up to whatever the use input. I think that's a mistake because the first index only needs to be 1 because I only have two dimensions to store values for, x and y. The second index should be 9 because the grid is only a 10 by 10 grid.
code:
int locationXY[1][9]
should cover both dimensions, x and y, and values up to 10. I think this is the right way to go about it?

This is probably a dumb question but, I've never read anything going into detail about it in the online tutorials I've read/watched, what is dangerous about any of this?

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.

Jose Valasquez posted:

When you are declaring an array the numbers are not 0 based, so you would want [2][10] in your example.

But if you have a 10x10 grid why wouldn't you have int locationXY[10][10]?

Jeffrey of YOSPOS posted:

Arrays are prove to out of bounds errors, my concern would be that you'd be given a firm rigid way of doing something, and only learn to manipulate that without understanding it. I see C as somewhat portable assembly language and is hard for me to think of it as a language whose abstractions I can trust without knowing how they work all the way.

So like, I suspect some XY problem here - what are you trying to store in a 2d array? How many values are you storing? 20 or 100 are both reasonable answers but you should be able to answer before you write any code.

If you want points on a n by n coordinate grid you want array[n][n]. If you want two arrays of size n, you could use an array[10][2], but I'd probably just use two arrays instead. It might be easier to post a (small) snippet of code and how it's defying expectations if you've already written something.

It's been a while since I've been able to use something other than a phone.

code:

    for(y=0;y<10;y++){
        for(x=0;x<10;x++){
            Location[x][y] = '.';
        }
    }

    printf("How many markers would you like?\n");
    scanf("%d", &markCount);

    for(a=0;a<markCount;a++){
        markData[a] = a+1;
        xLocData[a] = (rand() % 10);
        yLocData[a] = (rand() % 10);
    }

    for(a=0;a<markCount;a++){
        markCompareData[xLocData[a]][xLocData[a]];
    }
That is what I have, I was trying to combine the two arrays for x and y into a 2D array. Whenever I print the contents of markCompareData[][] it gives me a mess of numbers that are way bigger than anything I put into the array.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I had to take a break to handle some dumb work things and am back at learning C. Lately I've been having trouble with pointers and structs are there any good tutorials out there? I've watched plenty of youtube tutorials and was looking for something more.

Also out of curiosity why do people freak out about recursion so much?

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I can't seem to figure out how to pass a struct to a function as an array.

I have this:
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct QuizID{
    int QBank;
    float Weight;
    int Challenge;
    char Body[];
}AR_Quiz[10];

void GenerateQuiz(int num1, AR_Quiz[]);

int main()
{

    int HInput = 0;

    do{
        printf("Enter number of questions(1-10):  \n");
        scanf("%d", &HInput);
        if(HInput>10){
            printf("You have selected too many questions!\n");
        }else if(HInput<1){
            printf("You have selected too few questions!\n");
        }else{
            printf("You have selected %i questions.\n", HInput);
        }
    }while(HInput<1 || HInput>10);

    GenerateQuiz(HInput, AR_Quiz[HInput]); <- line providing the error

    return 0;
}

/*Function creating the user defined number of questions*/
void GenerateQuiz(int num1, AR_Quiz[]){
    int QCount = num1;

    AR_Quiz[QCount].QBank = 0; //place holder values
    AR_Quiz[QCount].Weight = 0.0;
    AR_Quiz[QCount].Challenge = 0;
    AR_Quiz[QCount].Body[] = 'A';

    if(QCount == 0){
        printf("Quiz generation complete.\n");
    }else{
        printf("Generated Question %i.\n", QCount);
        GenerateQuiz(QCount-1, AR_Quiz[QCount-1]);
    }
}
When I compile this it gives me "error: expected expression before 'AR_Quiz'. I'm not sure where the issue is I thought I could do the function call by value but, I am guessing pointers are what I should be doing because it's an array.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I've been trying to get GTK to work with code:blocks but, it seems that every error I fix results in new errors popping up. Is there a better way to set up GTK for C other than going through MSYS2 to get GTK?

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I haven't, I'm working in C so I didn't think Qt would work for it and its the first time I've heard of ImGui.

I take it, its probably better to keep to console until I move on to C++ then work with Qt?

E: Or is it possible for me to build a GUI in VS2017 then use my C code with it?

Azuth0667 fucked around with this message at 22:43 on Oct 20, 2018

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I wanted to learn and the OP mentioned that its generally better to start in C so I started and kept with it because I like the language. I intend to learn both.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
Is the C Programming Language by Kernighan and Ritchie still a good one to have for learning C?

I bought Beginning C by Ivor Horton, cheap, and it was good until it hit strings so I've been looking for something else.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.

elite_garbage_man posted:

You can certainly give gui's a try right now. An important skill to learn is bringing in 3rd party libraries, building and linking them, so it's better to learn it sooner rather than later imo. You don't want to have to code everything yourself just because you never learned how to link to static and dynamic libs. It's also done different ways using build chain tools like different ides, make, cmake etc... Some projects support multiple build tools while others only support one. It's definitely alot of grunt work at first, but the pay off iss pretty big considering you won't have to keep re-inventing the wheel.

Qt is unique in that is has a ton of tools included for design you gui visually, as well as having it's own ide. Be aware that the auto generated code results are pretty unorganized, but you still have the option of writing the code out from scratch. Imgui and juce are other great c++ gui libs too look at aswell. If you're looking for a c gui lib, check out nuklear. It's got a ton of great examples with and without graphics acceleration libraries making it pretty easy to pick up without a lot of extra cruft.

As for K&R, it's an ok book. There's nothing really much better though, other than the ones that feature chapters on modern c development which is probably something you won't want to concern yourself about as a new comer. This book has a brief intro for new comers as well as some sections on newer features included with the language that you may find interesting in the future: http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf

Also, I'm not quite sure I understand why you are forcing yourself to learn c if your using it as a stepping stone to c++. It might be better off just skipping ahead to c++ and learning your basics there, then getting into modern c++ features. But hey, you do you.

Thanks for the pointers.

I was originally going to use it as a stepping stone but, after putting 100 hours into it I really like the language so I'm sticking with it and going to add C++ eventually. I've got a colleague in my institution's math department that showed me some cool stuff that we could do with a bunch of data, that I thought was trash, once I get a better grasp of C so there's some more motivation.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I want to read text from a window in a program then use that information to submit keyboard input into an entry field in the same program.

Where should I look to start getting an idea how to do this?

E: I should clarify I'm using C.

Azuth0667 fucked around with this message at 18:11 on May 9, 2019

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.

nielsm posted:

On Windows? And is it a plain edit control and not rich text? Is it a native text control and not a "faked" one through some other GUI framework or even custom?

If it's a completely standard text box on Windows:
Use the FindWindowEx function to find the window and the window handle of the control in question.
Send a WM_GETTEXT message to the control to get its contents.
Then you can use SendInput to fake keyboard input, or if you just want to change a different edit box's contents maybe you can simply send it a WM_SETTEXT message.

If it's non-standard stuff your best bet short of doing OCR of screenshots is to try if the software responds to accessibility APIs and pretend you're a screen reader application.

Oops, forgot about that and yes it is Windows.

I'm not really sure how to answer the rest of that. From the UI it has one main window that is partitioned into other smaller windows of which all but, one are full of text. The remaining window looks like a long form answer field from a survey.

c0burn posted:

If this is more "how do I do x thing at my job quicker" than C coding, I'd do it in AutoHotkey or AutoIT

I tried autohotkey but, it doesn't seem to be able to read text from this program.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
I'm piloting a new virology course in the fall and I'm trying to make a game to provide an interactive example of how they interact with host cells. I'm not sure why but, for some reason in the DisplayMenu function when it calls DisplayStats the output is all garbage values. However, when DisplayStats is called in Main it shows the values that were assigned in GenerateVirus and DisplayUIBar also shows the assigned values. This is on windows and in C, what am I screwing up?

code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define TRUE 1
#define FALSE 0
#define tbBorder 85
#define MAX_NAME_SIZE 50

struct Virus{
    char Name[MAX_NAME_SIZE];
    int Number_T;
    int DNA;
    int RNA;
    float Genome_Size;
    int Strands;
    int Infections;
};

void DisplayMenu(struct Virus Human, struct Virus *pHuman, struct Virus *pEnemy);
void GenerateVirus(struct Virus *pEntity);
void DisplayStats(struct Virus Entity);
void DisplayUIBar(struct Virus Human);

int main()
{
    struct Virus Player;
    struct Virus *pPlayer = &Player;

    struct Virus Nemesis;
    struct Virus *pNemesis = &Nemesis;

     DisplayMenu(Player, pPlayer, pNemesis);
     DisplayStats(Player);
     DisplayUIBar(Player, Time);

     free(pPlayer);
     free(pNemesis);

    return 0;
}

void DisplayMenu(struct Virus Human, struct Virus *pHuman, struct Virus *pEnemy){

    int Option = 0;
    int LoopControl = TRUE;
    while(LoopControl == TRUE){

        printf("Menu:\n\n1>\tStart\n2>\tExit\n");
        scanf("%d", &Option);

        switch(Option){
            case 1:
                printf("\nPlace holder for starting a new game.\n\n");
                LoopControl = FALSE;
                GenerateVirus(pHuman);
                DisplayStats(Human);
                //GenerateVirus(&pEnemy);
                break;
            case 2:
                printf("\nExiting game.\n");
                exit(0);
            default:
                printf("\nInvalid Selection.\n\n");
                continue;
        }
    }
}

void GenerateVirus(struct Virus *pEntity){

    char TempName[MAX_NAME_SIZE];

    printf("Enter the name of your Virus (limit %d characters).\n", MAX_NAME_SIZE-1);
    scanf(" %s", TempName);

    strcpy(pEntity->Name, TempName);
    pEntity->Number_T = 21;
    pEntity->DNA = FALSE;
    pEntity->RNA = TRUE;
    pEntity->Genome_Size = 23000000.00f;
    pEntity->Strands = 1;
    pEntity->Infections = 0;
}

void DisplayStats(struct Virus Entity){

    printf("\nPlayer Statistics:\n\n");
    printf("Name:  %s\n", Entity.Name);
    printf("Number T:  %d.\n", Entity.Number_T);
    printf("Is DNA Virus:  %d.\n", Entity.DNA);
    printf("Is RNA Virus:  %d.\n", Entity.RNA);
    printf("Genome Size:  %f.\n", Entity.Genome_Size);
    printf("Number of Strands:  %d.\n", Entity.Strands);
    printf("Infections Caused:  %d.\n", Entity.Infections);
}

void DisplayUIBar(struct Virus Human){
        for(int i=0;i<tbBorder;i++){
            printf("-");
        }
        printf("\n");
        printf("|  Infections Caused:  %d | Genome Size:  %.2f |\n", Human.Infections, Human.Genome_Size);
        for(int i=0;i<tbBorder;i++){
            printf("-");
        }
        printf("\n");
}
E: This is the garbage output:

Player Statistics:

Name: ╨@
Number T: 4199040.
Is DNA Virus: 4199040.
Is RNA Virus: 0.
Genome Size: 2774421698444822856892921245859840.00.
Number of Strands: -2.
Infections Caused: 1997040570.

Azuth0667 fucked around with this message at 02:24 on Jun 22, 2019

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.

Thank you both, it looks like I still have a lot to learn when it comes to pointers and memory allocation.

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.
There's no way to pass a member of a structure to a function in c right? I have to pass a pointer to the structure instead?

Adbot
ADBOT LOVES YOU

Azuth0667
Sep 20, 2011

By the word of Zoroaster, no business decision is poor when it involves Ahura Mazda.

Absurd Alhazred posted:

You mean something like this?

C++ code:

typedef struct foo_ {
	int bar1, bar2;
} foo;

void someFunction()
{
	foo thingie;
	someOtherFunction(thingie.bar1);

}

Similar, I have:

code:
struct a{
     int x;
     int y;
     int z;
};

void ChangeLetter(struct a i, char v, int num){
     i->v = num;
}
I'd like to be able to pass i->v directly into the function and change it by num. I don't think I can do that though from what I've been reading.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply