-3

Below is a part of an algorithm i was given to use for a project, but as it's my first time to use an algorithm i don't understand the following lines. Please will need your help.

For i=1 to n do
     t[i] .mark <-- 0
     t[i] .num <-- -1
End
  • i don't understand the meaning of the dot – Ntirpang Louis Aug 22 '17 at 21:47
  • 6
    This isn't C and it makes little sense (without additional context). The best interpretation is that this is *pseudocode* for initializing an array of structs. – John Coleman Aug 22 '17 at 21:48
  • I consider it pseudo code for "in a loop from 1 to n, set the correspondingly indexed member of array named t, struct member named mark to 0, struct member named num to 1". It implies a data structure of array of structs. I assume that you should have learned those things in the course and you can now start coding. – Yunnosch Aug 22 '17 at 21:52
  • 1
    If I am right, it leaves to wonder what should happen to t[0]. It is not one of those teachers who count 1 and up, is it? – Yunnosch Aug 22 '17 at 21:54
  • 1
    @Yunnosch That might be intentional if the point of the homework problem is to translate something from pseudocode to C. Pseudocode is often 1-based, and an important skill is to be able to translate from 1-based pseudocode to 0-based code. – John Coleman Aug 22 '17 at 22:13
  • @JohnColeman Agreed. So adding an "assuming counting from 1 and array indices from 0" and then filling from 0 would demonstrate that ability by making clear a) noticed b) made intentional assumption as meaningful as possible. – Yunnosch Aug 22 '17 at 22:16

2 Answers2

2

This pseudo code can be translated to C

Use struct

struct cm{
    int mark;
    int num;
};


#define N 10

int main(void)
{

    struct cm t[N];

    for (int i=0;i<N;i++){
        t[i].mark = 0;
        t[i].num = -1;
    }   

    //print your struct elements field
    for (int i=0;i<N;i++){
        printf("%d: %d, %d\n",i ,t[i].mark, t[i].num);
    }

} 

We have an array of struct because of we need each element of it have two field of data (i.e. mark,num).

struct cm t[N]; define a N length array of structure cm.

In loop we assign to each field of array elements proper values.

For more readability you can use typedef instead of using struct to define your desire data structure in this case. typedef vs struct

Use typedef

typedef struct typecm{
    int mark;
    int num;
}typecm;


#define N 10

int main(void)
{

    typecm s[N];

    for (int i=0;i<N;i++){
        s[i].mark = 0;
        s[i].num = -1;
    }   

    //print values
    for (int i=0;i<N;i++){
        printf("%d: %d, %d\n",i ,s[i].mark, s[i].num);
    }   
}
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • `I < 9` should be `I < 10` (or even better, `I < n`). It's also unusual to use uppercase letters for variables in C. – vgru Aug 23 '17 at 13:46
  • I accept use n instead of hard coding 10. (9 is wrong thanks for catch my error). But i want to use uppercase letters for variables. suppose we have i,j as index, as these are very similar, I want to use I,K,M as loop iterator. I grab this idea from https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwj-t_ez3e3VAhUErRQKHdvwBMMQFggvMAE&url=https%3A%2F%2Fwww.goodreads.com%2Fbook%2Fshow%2F12394453-art-of-programming-contest&usg=AFQjCNFZLwdf6IRDpNhKlzJdCViHEnlpRg – EsmaeelE Aug 23 '17 at 16:03
  • Whichever coding convention works for you, it's fine, but keep in mind that programming for contests usually has quite opposite goals than programming for maintainability. I would never use the same casing for `struct` names (like `M`) and variables (like `I`), because casing is usually meant to differentiate the class of the identifier (struct, function, local variable, global variable). You could also argue that `I` is similar to `T`. – vgru Aug 23 '17 at 16:13
  • It seems true, and you say i must use small letters for all variable in `C` and only use capitals for Globals, constants, and etc – EsmaeelE Aug 23 '17 at 16:16
  • 1
    No, not specifically, people use various conventions (e.g. `struct SomeStruct`, `int variable = 5`, `void do_something(void)` or `void doSomething()`, caps are usually macros like `#define MAX_NUM 5`, etc). As you can see [here](https://stackoverflow.com/q/1722112/69809), for example, everyone has a different idea, but is always to differentiate different symbols *somehow* (but variables are almost universally lowercase in all the sources I've seen so far). But again, if your company has a certain coding standard, it's what you should use to make all code consistent. – vgru Aug 23 '17 at 17:52
  • @Groo first is my edition on post correct both in logical and coding style. second if you suggest to use special coding style for `C`, i am not restricted to use some special style, i code for hobby and learn. – EsmaeelE Aug 23 '17 at 18:05
1

The "t" seems to be an array of objects, and "mark" and "num" are properties of the object. This may help you: From an array of objects, extract value of a property as array

mruanova
  • 6,351
  • 6
  • 37
  • 55