Brand New To C Programming

Posts 1–6 of 6 · Page 1 of 1
Brand New To C Programming
Hey guys, I'm brand new to C programming.
I'm currently a Sophomore in University and I just changed my major to Computer Science.
So the thing is, we're working on very basic things right now (not to me, but I'm sure it's basic to you guys).

Is there anywhere online where I can get some practice type problems? I feel like I'm falling behind a little bit, and I need some more practice.
If need be I can post some code of the most recent program I've done just let me know if that would help find me something to practice.
I've tried googling to find some busy work problems for my level but I couldn't find any.
Thanks in advanced to anyone who helps me!
Pick a book from this sticky, get a copy, do the exercises from the chapters. What kind of basic stuff are you doing?

You could just make up some project to do that's relevant to the stuff you're struggling on.
Quote Originally Posted by InunoTaishou View Post
Pick a book from this sticky, get a copy, do the exercises from the chapters. What kind of basic stuff are you doing?

You could just make up some project to do that's relevant to the stuff you're struggling on.
I just finished this code yesterday, with the EXTENSIVE help of a friend of mine who is in higher level Computer Science courses.
Code:
#include <stdio.h>

int main() {
	int num, secretCode, cow = 0, bull = 0, guess[6], game = 0;
	char guessString[6];

	printf("How many digits do you want the code to be? (2 - 6)\n");

	do
	{
		scanf("%d", &num);
		if (num < 2 || num > 6)
		{
			printf("Incorrect. Enter a number between 2 - 6.\n");
		}
		

	} while (num < 2 || num > 6);


	while (game == 0) {
		cow = 0;
		printf("Guess the code: ");
		scanf("%s", guessString);
		for(int i = 0; i < 5; i++) {
			guess[i] = guessString[i]-'0';
		}

		if (num == 2) {
			secretCode = 12;
			if (guess[0] == 1 && guess[1] == 2) {
				game = 1;
			}
			if (guess[0] == 1) {
				cow++;
			}
			if (guess[1] == 2) {
				cow++;
			}
				bull = 2 - cow;
			if (game == 0)
				printf("Nope! %i Cows, %i Bulls.\n", cow, bull);
		}

		if (num == 3) {
			secretCode = 123;
			if (guess[0] == 1 && guess[1] == 2 && guess[2] == 3) {
				game = 1;
			}
			if (guess[0] == 1) {
				cow++;
			}
			if (guess[1] == 2) {
				cow++;
			}
			if (guess[2] == 3) {
				cow++;
			}
				bull = 3 - cow;
			if (game == 0)
				printf("Nope! %i Cows, %i Bulls.\n", cow, bull);
		}

		if (num == 4) {
			secretCode = 1234;
			if (guess[0] == 1 && guess[1] == 2 && guess[2] == 3 && guess[3] == 4) {
				game = 1;
			}
			if (guess[0] == 1) {
				cow++;
			}
			if (guess[1] == 2) {
				cow++;
			}
			if (guess[2] == 3) {
				cow++;
			}
			if (guess[3] == 4) {
				cow++;
			}
				bull = 4 - cow;
			if (game == 0)
				printf("Nope! %i Cows, %i Bulls.\n", cow, bull);
		}

		if (num == 5) {
			secretCode = 12345; 
			if (guess[0] == 1 && guess[1] == 2 && guess[2] == 3 && guess[3] == 4 && guess[4] == 5) {
				game = 1;
			}
			if (guess[0] == 1) {
				cow++;
			}
			if (guess[1] == 2) {
				cow++;
			}
			if (guess[2] == 3) {
				cow++;
			}
			if (guess[3] == 4) {
				cow++;
			}
			if (guess[4] == 5) {
				cow++;
			}
				bull = 5 - cow;
			if (game == 0)
				printf("Nope! %i Cows, %i Bulls.\n", cow, bull);
		}

		if (num == 6) {
			secretCode = 123456;
			if (guess[0] == 1 && guess[1] == 2 && guess[2] == 3 && guess[3] == 4 && guess[4] == 5 && guess[5] == 6) {
				game = 1;
			}
			if (guess[0] == 1) {
				cow++;
			}
			if (guess[1] == 2) {
				cow++;
			}
			if (guess[2] == 3) {
				cow++;
			}
			if (guess[3] == 4) {
				cow++;
			}
			if (guess[4] == 5) {
				cow++;
			}
			if (guess[5] == 6) {
				cow++;
			}
				bull = 6 - cow;
			if (game == 0)
				printf("Nope! %i Cows, %i Bulls.\n", cow, bull);
		}
	}

	printf("You won!\n");


	return 0;
}
This is designed to be a guessing type game relative to the actual "Bulls and Cows" game.
It was an assignment that my computer science instructor gave us.

I'll continue to read the text book and keep trying the problems in there, and I'll check on that introduction book you have in your thread. Thanks man.
Maybe your instructor can give you some pointers too - sure he'll appreciate you showing interest
Alright, so that's entry level programming. The first couple of chapters for the beginner books goes over data types (you'll use char[] or char* instead of std::string), working with cin/cout (that's C++, you'll use scanf/printf), and I believe statements (if/while/do/for. The books are C++ but it's still relevant. You should have also gotten a CS book for your class, your book might have exercises in it to do too, check that out.
as practice, you could make the 'password' something that is entered by another user.

'secretcode' is unused and could be removed : P
'guess[]' is an array of int's , but is only used to store chars.
most any time you see code that repeats, it could/should be in a loop...when you go to edit it, you only edit in 1 place, and don't make the mistake of forgetting to edit all occurrences.
printf() with no formatting characters could be replaced with puts().
If the first check (comparing the whole password), is found to be true, no need to run the next cluster of if()'s comparing each individual char.

Not criticism, just stuff to think about. We all started exactly where you are : ) The point is to keep learning.

edit: I don't have any to recommend, but search around https://www.google.com/?gws_rd=ssl#q...+problems+easy
When you find ones that you think you can do, try them. I'm sure most of them post (possible) solutions, though many might not be in 'C'...it's generally regarded as a lower-level/system-programming language, so it's not as prevalent for beginner type stuff.
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us