[QUOTE=WaltP;1374475]Answering a C question with C++ is worthless. And using gets() and getch() is so wrong it's close to criminal.

And do I really need to mention CODE Tags? The Member Rules are quite clear...[/QUOTE]What I find to be even more disturbing is the fact that this guy's created an account for improperly answering this question.

Thanks a lot, it worked!

This one is pretty strange (or at least I can't figure out what triggers it). I have the piece of code below, but there seems to be a bug with the second call of [icode]scanf[/icode]. It simply gets skipped. So the input should be something like
[code]
10 3
fbfbfbfbfb
3 1 2 3
3 4 5 6
4 7 8 9 10
[/code]
but after I enter
[code]
10 3
fbfbfbfbfb
[/code]
I get the (wrong) result, I do not get asked for the other 3 lines. Thanks in advance!

P.S.: The variables' names are not in English :)

[code=C]

include <stdio.h>
define N 200

int main(void) {

unsigned short n, k, i, j, elevi_grupa, elev_curent, 
               fete = 0, baieti = 0, corect = 1;
int sir[N];

scanf("%hu %hu", &n, &k);

for(i = 0; i < n; ++i)
    sir[i] = getchar();

for(i = 0; i < k; ++i) {

    scanf("%hu", &elevi_grupa);

    if(elevi_grupa < n % k || elevi_grupa > n % k + 1)
        corect = 0;

    for(j = 0; j < elevi_grupa; ++j) {

        scanf("%hu", &elev_curent);

        if(sir[elev_curent - 1] == 'f')
            ++fete;
        else
            ++baieti;
    }

    if(fete - baieti < -1 || fete - baieti > 1)
        corect = 0;

    printf("%hu %hu\n", baieti, fete);
    fete = 0;
    baieti = 0;
}

if(corect)
    printf("DA");
else
    printf("NU");

return 0;

}
[/code]

The code above was not really the finished version, I kept working on it after posting. A big source of errors might be given by the character set on one's computer. If the characters 0 to 9 are not continuous, 2 + 4 might translate into something as bad as 32947 - 20492180.

The main problem with your code is that the arrays should not be passed using the address operator (&). So if you wanted to pass the array to your [icode]countEven[/icode] function, you'd use the following
[code=C]
countEven(numbers);
[/code]
This is mainly because the name of the array is a pointer to its first element.

The second problem is that you've forgotten to put the j in your function's return statement.

Last but not least, the [icode]size[/icode] and [icode]j[/icode] variables inside [icode]main[/icode] are not used, so you can get rid of them.

Here's how I'd write the above piece of code
[code=C]#include <stdio.h>

int countEven (int *numbers)
{
int even[7];
int i=0, j=0;

for(; i < 7; ++i)
    if(!(numbers[i] % 2))
        even[j++]=numbers[i];

return j;

}

int main (void)
{
int numbers[] = {2, 5, 10, 16, 31, 17, 30};

countEven(numbers);

printf ("%d\n", countEven(numbers));

return 0;

}
[/code]

ybsolar commented: there's a nice FOR loop. +0

Well... most everyone I know has heard of and uses WinRAR on Windows. It [i]is[/i] free, for non-commercial use. You do get a message asking you to buy it after a period of time if you use it commercially. Otherwise, you just click Cancel and it still works, fully-featured.

It's just like TeamViewer for iPhone, for example. There's a commercial version for $100 and a free version for non-commercial use.

Why WinZIP? WinRAR si free for non-commercial use and reads ZIP, RAR, 7-zip, and so on.

You could have a look over [url=http://zlib.net]zlib[/url], I think it's your best bet.

dragonpunch commented: nice link +0

[quote=Adak]It's easier if you do your addition and subtraction on these long arrays, just as you were taught to do them, in school.[/quote]
Hmm... I thought this is what I was doing. Or are you talking about some other manner? Basically, I go to the last digits of both, add them to the previous quotient (initially 0), store the remainder of their addition into the [icode]sum[/icode] array, and store their quotient in another variable for later use (next iteration).

[quote=Adak]Some people like to flip the numbers around[/quote]I've found this to be unnecessary, and taking some added time to implement.

[quote=Adak]but I like just using char's[/quote]Ok, but if I used [icode]char[/icode]s, and later I wanted to, say, divide the number by two, wouldn't it require some extra work? I wanted to do the conversion [i]on the spot[/i], so to speak.

Hmm... apparently, changing the [icode]unsigned short[/icode] declaraction to [icode]int[/icode] did the trick. Also, if after the loop ends the quotient is greater than 0, it should be added to the array.

When someone explains why [icode]unsigned short[/icode] does not work, I'll mark the thread as solved. Thanks in advance!

Also, here's the final piece of code, should anyone be interested
[code=C]

include <stdio.h>
define NR_DIG 100
define EOL (int) '\n'
define CHR (int) '0'

int main(void) {

int a[NR_DIG], b[NR_DIG], sum[NR_DIG + 1], 
    a_len = 0, b_len = 0, sum_len = 0, aux, qtt = 0, ch, i;

printf("a: ");
while((ch = getchar()) != EOL)
    a[a_len++] = ch - CHR;

printf("b: ");
while((ch = getchar()) != EOL)
    b[b_len++] = ch - CHR;

for(i = 1; i <= a_len || i <= b_len; ++i) {
    aux = (a[a_len - i] + b[b_len - i] + qtt);
    sum[i - 1] = aux % 10;
    ++sum_len;
    qtt = aux / 10;
}

if(qtt > 0)
    sum[i - 1] = qtt;

for(i = sum_len; i >= 0; --i)
    printf("%d", sum[i]);
printf("\n");

return 0;

}
[/code]

Yes, it's yet another thread about implementing three basic operations (addition, subtraction and multiplication) on abnormally large numbers (> 100 digits).

I've stored each digit inside an array of [icode]int[/icode]s, as the following piece of code illustrates
[code=C]
while((ch = getchar()) != EOL)
a[len_a++] = ch - CHR;
[/code]

Now, when it comes to adding them, I add the [i](len_number - k)[/i]th digits of both numbers, compute their reminder and their quotient, and store the reminder on the [i]k[/i]th place of the [icode]sum[/icode] array. The following piece of code works nicely for, say, 132 and 32, but when I try adding 1248 and 2, the result 1950, instead of 1350. I think I'm doing some illegal work on the arrays (at some point, I have something like [icode]b[1 - 3][/icode]), but I've been struggling for a lot of time and I still can't figure things out, so any help would be highly appreciated.
[code=C]
for(i = 1; i <= len_a || i <= len_b; ++i) {

aux = (a[len_a - i] + b[len_b - i] + qtt);
rem = aux % 10;
sum[i - 1] = rem;
++len_sum;
qtt = aux / 10;

}
[/code]

And here's the big picture
[code=C]

include <stdio.h>
define NR_DIG 100
define EOL '\n'
define CHR '0'

int main(void) {

unsigned short a[NR_DIG], b[NR_DIG], sum[NR_DIG + 1];
int ch, len_a = 0, len_b = 0, len_sum = 0, rem, qtt = 0, i, aux;

printf("a: ");
while((ch = getchar()) != EOL)
    a[len_a++] = ch - ...

You simple write its name and pass it the arguments. For example:
[code=C]

include <stdio.h>

int main(void) {

/* printf *is* a function, and that's how we call all the other functions */
printf("I am %d years old\n", 15);

return 0;

}
[/code]

Going a step further and tokenizing the function call, we get

[icode]printf[/icode] -- the function's name
[icode]"I am %d years old\n"[/icode] -- the function's first argument
[icode]15[/icode] -- the function's second argument

There are three ways you can do this. The first one (and the not-so-basic one) is making use of [i]pointers[/i]. You can declare the cross-function variables inside main, and pass their addresses to the other functions, which, in turn, will modify the initial variables. Here's a sample, but unless you understand pointers, you won't make much of it:
[code=C]

include <stdio.h>

void double_it(int *);

int main(void) { / Please define main as int instead of void /

/* I'm declaring and initializing i */
int i = 2;

/* I'm calling the double_it function, passing it the address of i instead of its value */
double_it(&i);

/* I'm now printing the variable. It should be 12 */
printf("i: %d\n", i);

return 0;

}

/ Definition of double_it /
void double_it(int *n) {

/* Notice the use of pointers instead of normal variables */
*n = *n * 2;

}
[/code]

Perhaps the easiest way to do it would be to declare the cross-function variables as [i]global[/i] (outside of [icode]main[/icode]). This way, when a function alters the variable declared as global, the actual value is changed.
[code=C]

include <stdio.h>

int n;

void double_it(void);

int main(void) {

/* Call double_it, changing the value of n */
double_it();

printf("n: %d\n", n);

return 0;

}

void double_it(void) {

n *= 2;

}
[/code]

Last but not least, you can always return the modified value and reassign it.
[code]

include <stdio.h>

int double_it(int );

int main(void) {

int n;

n = double_it(n);

printf("n: %d\n", n); ...

It actually explicitly states that the assignment to [icode]i[/icode] is of [icode]unsigned long[/icode] type. Given the piece of code you've posted above, if you use something like [url=http://splint.org/]splint[/url] to check your code, you get a warning, stating that the assignment to [icode]i[/icode] is of incompatible type.

There are few cases I can think of in which the suffix really is helpful, but I remember something about a floating-point constant being treated like a [icode]double[/icode] by default, suffixing it with [icode]f[/icode] making it of [icode]float[/icode] type.

You could create a header file. That is, take all the function definitions and prototypes from the first C file, put them in another .h file, and then use
[icode]#include "header_file.h"[/icode] after you include the standard headers in your second C program. For example
header.h
[code=C]

include <stdio.h>

void my_printf(const char *message) {

printf("%s\n", message);

}
[/code]

main.c
[code=C]

include <stdlib.h>
include "header.h"

int main(void) {

/* Beginning of epic code */
if(1 == 1)
    my_printf("Hello, World");
else
    exit(EXIT_FAILURE);
/* End of epic code */

return 0;

}
[/code]

Now, to develop a little on the [icode]#include[/icode] directive, if you use angle brackets after it, the preprocessor looks inside a folder containing all the header files which came with the compiler and replaces the line with the contents of the specified header file. If you use double quotes, on the other hand, the preprocessor scans the path given between the quotation marks. For example, if you wanted to include a file inside a [i]headers[/i] folder in the current directory, you would use
[code=C]

include "headers/file.h"

[/code]
Other than that, the [icode]#include[/icode] directive works the same in both cases.

I hope this helps.

If you're looking for the Assembly code and are using gcc, use the -S command line argument, and the output file will contain the code.

I agree with Narue that [url=http://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504/ref=sr_1_1?ie=UTF8&s=books&qid=1287055790&sr=1-1]C Programming: A Modern Approach[/url] is [i]the[/i] book for learning C. That's how I've learnt it, and I'm pretty satisfied with what I've got under my belt.

Now, there's also [url=http://www.amazon.com/Without-Fear-Beginners-Guide-Makes/dp/0321246950/ref=sr_1_1?ie=UTF8&s=books&qid=1287055864&sr=1-1]C++ Without Fear[/url]. Ok, it's on C++, but I've found it to be quite the most friendly introduction to C++ programming.

Why [icode]scanf[/icode]? Are you familiar with [icode]getchar[/icode]? It's better suited for your needs. With [icode]getchar[/icode], it's as simple as
[code]
while(c = getchar() != '\n')
[/code]

Also, if I remember correctly, using a [icode]char[/icode] to check for EOF is not going to work, for [icode]char[/icode] is an unsigned data type, while EOF is a negative one.

Because in C, const != "constant", but "read-only". That is, it is legal to initialize a const variable using
[code]
void f(int n) {

const int m = n / 2;

}
[/code]
but its value is dependent of n. So if we call f passing first 2 as argument, and then 4, the values of m will differ. But a switch statement requires integer constants which are known before the program executes. In the above example, this is clearly not the case.

Some other good books are the ones in O'Reilly's [i]In a Nutshell[/i] series. [url=http://oreilly.com/catalog/9780596006976/]This[/url] is the one on C. I haven't read it yet, but I plan to change this pretty soon.

Apart from Xcode hiding a lot from you, you will quickly find that Vim is a better editor than the ones integrated with the IDEs. And Xcode is more useful for projects, while right now you are only getting your feet wet with C, so, as you've already found out, there's a lot of bulk in for people just starting out.

In the end, the choice is yours, so choose the one you feel the most comfortable with.

The program is [b][color=red]wrong[/color][/b]. Here's why:

First of all, you are not including a header file, therefore, you have no access to the I/O functions (i.e. [icode]scanf[/icode]).

Next, you define [icode]main[/icode] as [icode]void main()[/icode], when it should be [icode]int main(void)[/icode].

Furthermore, you write [icode]scanf("%d", a);[/icode]. This is incorrect, as the value of [icode]a[/icode] must be changed by the function. Therefore, you must feed it a [i]pointer to a[/i]: [icode]scanf("%d", &a);[/icode].

You don't need braces around the statements inside a [i]case[/i] statement. So, instead of
[icode]
case 1: {
printf("You are happy.");
break;
}
[/icode]
we write
[icode]
case 1:
printf("You are happy.");
break;
[/icode]

Last but not least, your function is not returning anything. It should return 0 upon successful execution. So instead of [icode]getch();[/icode], use [icode]return 0;[/icode].

Read a good book on it, but it might be a little too expensive for just file I/O. If you're looking for a reference, check the Dinkumware link in my signature.

[QUOTE=abhiab1991;1346887]1) Write a program to count the number of blank spaces in a given string.
2) Write a program to abbreviate a given string (e.g. if the input is Amar Akbar Anthony, the output will be AAA)[/QUOTE]

  1. Go through the string, and increment a counter when you encounter a blank space.
  2. Go through the string, and store the first letter of each word (let the word be a string of letters between blank spaces) inside an array of characters.

You need to find out how to do the above in C. For this, you need some knowledge of how strings are stored, how to loop through a string (and how to look for the end of a string), and conditional statements. I can give you the highlight for the first program:
[code]

include <stdio.h>

int main(void) {

/* Declare the variables */

/* Get the string */

/* Go through the string */
    /* If you encounter a white space */
        /* Increment the counter */

return 0;

}
[/code]

Replace the comments with actual code. Write the comments version for the second task, and then replace the comments with actual code.

No, you just have to change the linking. Before I tell you how to do this, though, I recommend you switch to Terminal + MacVim. Using an IDE in the beginning is overkill, as well as hiding certain aspects away from you.

Now, after you create a new file (again, if you're just starting out, I suggest you drop the .h file; that is, uncheck the option), you should see [url=http://drp.ly/NeNM]this[/url] in your project's main window. Just deselect the old file (in my case, main.c), and it should work. The build error is triggered by the linker, as you end up having two definitions of the same function (main).

Was it really necessary that you bumped it?

An [b]abstract data type[/b] is a data type whose representation is hidden from the client.

A [b]heap[/b] is an array object representing an incomplete binary tree.

A [b]stack[/b] is a set of items in which the element to be deleted is known in advance, specifically, the last element inserted into the set. That's why it's said to implement a [i]LIFO (last-in, first-out)[/i] policy.

The [b]preprocessor[/b] is a piece of software which takes care of editing a file by replacing directives before the program gets compiled. The directives are [icode]#define[/icode], [icode]#include[/icode], [icode]#error[/icode], [icode]#line[/icode], [icode]#pragma[/icode].

A good first step would be reading [url=http://en.wikipedia.org/wiki/Bus_error]the Wikipedia article[/url]. If you still don't manage to solve the problem, post some code here.

A [i]parameter[/i] is used when defining/declaring (or prototyping) a function, and an [i]argument[/i] is passed (or given) when calling the function. In the example below, [icode]x[/icode] and [icode]y[/icode] are parameters, while [icode]a[/icode] and [icode]b[/icode] are arguments:
[code]
double average(int x, int y); / Function prototyping; x and y are parameters /

int main(void) {

int a = 10, b = 20;

printf("%d\n", average(a, b); /* The function is called; a and b are arguments */

}

double average(int x, int y) { / Function definition; x and y are parameters /

return (x + y) / 2;

}
[/code]

The ## operator [i]pastes[/i] two [i]tokens[/i] together (this is why the operation is called the [i]token-pasting[/i] operation). In the above code, a function is defined as a macro, which, when given two arguments (var and 12, in our case), forms a single token (in our case, an [i]identifier[/i]), var12.

It's written inside the box where you type your reply, in gray.