[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Anyone good at C Programming? I totally need help writing

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 54
Thread images: 9

File: C.jpg (18KB, 800x450px)
C.jpg
18KB, 800x450px
Anyone good at C Programming? I totally need help writing a code for a problem.
>>
>>56827299
why dont you post the code and one of the big guys will help you.
>>
No, but, if you need help writing a problem for a code, I'm your man.
>>
>>56827332
I am not sure where to start. I have written a few codes before, but I am so lost where to even begin with the ones I have.
>>56827334
Yes definitely! I'm going to post the problem here in just a minute
>>
how one can create dynamic array of char* using realloc()?
>>
I need to write a program using input and output files with the names lab6p1.in and lab6p2.out.

As part of my solution, I need to write and call the function "leap()".

Question the program needs answered is "The year 1900 is not a leap year, whereas the year 2000 is a leap year. Why?"

The input file (lab6p1.in) will have three integers which are 6 26 and 2000.

Output file needs to contain the day, number, year (in a format similar to a|a|a or 02|28|1900)
>>
>>56827586
I am about to post a code I've written that may give an idea of what I mean by some of this.
>>
sent ;)

check your PMs
>>
>>56827611
Having no luck, can't find :(
>>
File: Speedofsound.jpg (173KB, 945x768px)
Speedofsound.jpg
173KB, 945x768px
>>56827586
>>
>>56827810
Here's an idea of what i
t's like. This the last one I did.
>>
>>56827581
realloc() doesn't create anything
malloc does. Let's say you need a 10 chars array.
char *c = (char *)malloc(sizeof(char) * 10);

you get a pointer to your array.

if you want to change the size of said array, THEN use realloc();
char *c = (char *)realloc(c, 20);

don't forget to:
- clean/initialize your arrays prior to using them and
- use free() when you're done.
>>
>>56827963
I meant to write
char *c = (char *)realloc(c, sizeof(char) * 20);

my bad.
>>
>>56827586
What the fuck are you actually trying to do and what has it got to do with:
>"The year 1900 is not a leap year, whereas the year 2000 is a leap year. Why?"
>>
>>56827963
>>56827991
It's worth noting that modern C doesn't require you to cast malloc's return value.
>>
>>56828012
Think about the logic inside function leap(). How do you write a
condition to check if a year (an integer) is divisible by 4? By 100? By 400? For
which of these 3 conditions is a year “always” a leap year? For which of these 3
conditions is it “sometimes” a leap year?

Maybe I should choose another problem for help cause this one just seems too much
>>
>>56828189
Ask the user for the number N (an integer) of real numbers in the set. Then ask for
each number, one at a time. Below is a sample run:


Statistics CalculatorInput N: 5
Input num: 71.7
Input num: 78.5
Input num: 80.9
Input num: 81.3
Input num: 77.4
Average = xxx.xxx
StanDev = yyy.yyy
>>
>>56828189
>How do you write a
>condition to check if a year (an integer) is divisible by 4?
if((year % 4) == 0) { // it's divisible
} else { // it's not
}
>>
File: smugtrump.jpg (18KB, 326x326px)
smugtrump.jpg
18KB, 326x326px
>>56827334
>>56827483
>>
>>56828154
it's also worth noting that
sizeof(char) * 20
and 20
are the same since sizeof(char) is 1.
>>
>>56828311
Damn this is perfect. So for the divisibility we're using else if statements?
>>
>>56828434
>since sizeof(char) is 1
What if we are going to use 16-bit encodings?
>>
>>56828519
If the compiler you're using is C99 compliant, it's always 1.

From the C99 specs (regarding sizeof):
When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.
>>
File: LAB.jpg (189KB, 1366x768px)
LAB.jpg
189KB, 1366x768px
Here is a screenshot of the way it was written out by the professor
>>
File: LABB.jpg (138KB, 1366x768px)
LABB.jpg
138KB, 1366x768px
>>56828571
>>
>>56828571
Algorithm taken from wikipedia.
#include <stdio.h>

int leap(int);

int main(int argc, char **argv) {
int year;

for(year = 1; year <= 3000; year++) {

fprintf( stdout, "%d is %s\n", year, (leap(year) ? "a leap year" : "NOT a leap year") );

}

return 0;
}

int leap(int y) {
if((y % 4) != 0) return 0;
else if ((y % 100) != 0) return 1;
else if ((y % 400) != 0) return 0;
else return 1;
}
>>
>>56828571
>>56828589
>Windows 10 user
>Can't export as a single .png
Retardation checks out
>>
>>56828571
Use dynamic predicates
>>
>>56828443
you should always when you've decisions. it's the way to handle branching paths.
>>
>>56829616
Awesome!
>>
>>56828883
Well to be fair this is where I had it and I had a screen shot ready for a classmate I shared it with, plus who cares if I use 10? Does it bother you THAT much? How does it relate to C code when I don't use it on 10? Must be rough that your only contribution is the obvious.
>>
Anyway, thank you all for helping me out, especially one anon in specific for getting me the algorithm and stating to use else if statements.
>>
Why doesn't this work?

long long int a =  600851475143;
long long int b = round(sqrt(a))
>>
>>56831397
works on my machine
>>
File: 1454454996973.png (630KB, 670x320px)
1454454996973.png
630KB, 670x320px
>>56831694
:|

I get pic related after compiling and running.
>>
>>56831694
>>56831848
Also, when I put the number directly inside the functions, it works.
>>
>>56831881
Did you include math?
>>
>>56831848
idk how it works with your program but with my program when running and there's include <math.h> then you have enter -lm at the end before compiling. That's usually what it means when it states undefined reference to sqrt
>>
I'm taking a C class at a City College. I wish they did more than just teach the syntax. I read K&R before the class began, and now I'm about a month ahead of everyone else. I really wish we spent more time learning algorithms and less time writing stupid fucking conversion programs. This assignment is somewhat interesting, learning how to parse through HTML, mainly using strstr(). Please remember to like, comment, and subscribe to my blog desu
>>
File: compile.jpg (93KB, 881x768px)
compile.jpg
93KB, 881x768px
>>56831848
>>
>>56831893
Yeah

>>56831961
Damn, that actually fixed it, thanks

>>56832072
y-you too
>>
>>56832086
No problem!
>>
>>56832105
btw, what does -lm do anyways?
>>
>>56832120
Pretty much it just tells the program/compiler to compute specific mathematical formulas such ln, sqrt, pow, etc. but you don't need it when you use very basic math like addition, subtraction, etc.
>>
>>56832153
ah ok, thanks
>>
>>56832153
Why do you need to link to the math library anyway? Is it not part of the standard library?
>>
>>56832320
no
>>
>>56828519
>>56828567
These aren't mutually exclusive though. sizeof(char) is always 1, but CHAR_BIT could be 16.
>>
>>56832798
>CHAR_BIT could be 16.
What? Why would someone do this? How common is this? I've always parsed files 1 char at a time under the assumption that it would be 8 bits.
>>
>>56832072
>.exe
Why?
>>
Why is it that when I try to free a region of memory after changing it via realloc I get a segfault?
>>
>>56831397
Semicolon?
>>
>>56833452
My guess is that you're freeing the wrong memory.
Post code snippet.
>>
>>56834673
Too late, I turned in the assignment.

In short, I was trying to prepend a string to an array of strings. So I realloced the array to make room for one pointer,
Strcpy'd the strings after reallocating the memory each element of the array pointed to. Then setting array[0] to a specific string via strcpy.
Free(array[0]) broke. Claimed the pointer was invalid.

The weird thin was that it worked when the array was length 1 or 3, but not 2 or 4
Thread posts: 54
Thread images: 9


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

If you need a post removed click on it's [Report] button and follow the instruction.
If you like this website please support us by donating with Bitcoin at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties. Posts and uploaded images are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that website. If you need information about a Poster - contact 4chan. This project is not affiliated in any way with 4chan.