-2

i want to create this simple program using for loop. Now what it should do is get a value from an array and print it corresponding to the day(1-30).

Here is my code.

#include<stdio.h>

int main() {

    int j;
    int days=1;
    int value[]={31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
    
    for(days=1; days<=31; days++){
        
            for(j=value[0]; j<=value[].length; j++){
                printf("%d \t %d\n", days, j);  
            }
        
        
        }
}

when i run this the value[] doesnt change... i believe im having trouble incrementing the index of array...

somebody help... how do i do it???

  • 3
    This `j<=value[].length]` is illegal C. Make sure your compiler is properly configured. Try `#ifdef __cplusplus` / `#error misconfigured compiler` / `#endif`. Also turn on and **mind all warnings**. – pmg Jul 01 '20 at 07:05
  • 1
    The algorithm of the inner `for` loop is messy even if you attempt to use C++ features. Think about it twice. – RobertS supports Monica Cellio Jul 01 '20 at 07:14
  • "when i run this ..." You cannot possibly run this, because compiling will fail. Since you do however describe that something is happening when running you obviously have not provided a [mre] of the actual code you are working on (or better an MRE which demonstrates your problem with minimal code). Please edit so that the code you are discussing the runtime misbehaviour of is actually compilable. – Yunnosch Jul 04 '20 at 06:08

3 Answers3

1

First of all, value[].length] is not permissible in C to determine the length of an array. As side note there is also an abandoned ] after length which would give you a syntax error, even if you would use a C++ compiler to compile this code.

If you want to get the amount of elements you need to use f.e. the sizeof operator and divide the amount of allocated memory in bytes by the memory per element object:

sizeof(value) / sizeof(value[0])

Furthermore the algorithm of the inner for loop

j = value[0]; j <= value[].length; j++){

makes no sense. Why would you want to compare the value at a certain array element with the length of the whole entire array instead of to iterate over the array until the matched value is found?

Think about what you are doing!

You also need another if check to proof it you encountered the right value.

For example:

#include <stdio.h>

int main (void) {

    unsigned int j;
    unsigned int days = 1;
    unsigned int value[] = { 
                             31,30,29,28,27,26,
                             25,24,23,22,21,20,
                             19,18,17,16,15,14,
                             13,12,11,10,9,8,7,
                             6,5,4,3,2,1
                           };

    unsigned int len = sizeof(value) / sizeof(value[0]);

    printf("Day \t Index \t Value\n");
    
    for (days = 1; days <= 31; days++) {
         for (j = 0; j < len ; j++) {
              if (value[j] == days)
                   printf("%u \t %u \t %u\n", days, j, days);  
         }        
    }
}

Output:

Day   Index  Value
1     30     1
2     29     2
3     28     3
4     27     4
5     26     5
6     25     6
7     24     7
8     23     8
9     22     9
10    21     10
11    20     11
12    19     12
13    18     13
14    17     14
15    16     15
16    15     16
17    14     17
18    13     18
19    12     19
20    11     20
21    10     21
22    9      22
23    8      23
24    7      24
25    6      25
26    5      26
27    4      27
28    3      28
29    2      29
30    1      30
31    0      31

Side Notes:

  • I used unsigned int because it is unusual that you have negative days and it is better for the comparison with the return of the sizeof operation to determine the length of the array value.

  • The formatting of the output can be different on your implementation because how many spaces \t is exactly, is implementation-specific.

  • Use a C compiler to compile C code and never ignore compiler warnings. Don't intermix C with C++ code.

  • Use int main(void) instead of int main(). The latter is not strict C standard-compliant.

  • Good and free C starting books are Modern C or The C Programming Language (2nd Edition). These and others you can find here:

    The Definitive C Book Guide and List

  • i think i confused you all because i gave the value as reverse order from 31.....actually i just want the first value form array to show beside day 1, similarly 2nd value beside day 2.. Day 1----first value Day 2----Second value Day 3----Third value and so on..... – Bishal Neopaney Jul 01 '20 at 08:43
  • @BishalNeopaney Even if it's hard, I won't help you further here because this would in the end of it all bring you further, which I don't want. I answered your question which has an obvious suggestion to find the index of an value corresponding to the relative day. Maybe this is even the real task to do and you misinterpret the task. To just print the values of an array in the order in which they are initialized is a relatively simple task. You should know how to do this after the first few chapters of a C starting book. I provided all the information you need to find the solution by yourself. – RobertS supports Monica Cellio Jul 01 '20 at 12:22
  • @BishalNeopaney In fact, under usual circumstances I hadn't answered your question because it lacks of details and clarity. But I answered because I saw your effort to find a solution for the supposed task. Please understand that it is better for you to find this solution yourself. I provided all what you need to solve it. – RobertS supports Monica Cellio Jul 01 '20 at 12:22
  • @BishalNeopaney I added a book recommendation of mine to the answer. If you still have problems to find a solution, read the respective chapters in these books. But as I said, there is also a possibility that you misunderstood the task given to you, for me, the one I did is way more plausible. – RobertS supports Monica Cellio Jul 01 '20 at 12:25
  • thank u so much...it is indeed a relatively simple task and i found out what i was looking for.... thank u for looking in to it... – Bishal Neopaney Jul 02 '20 at 04:29
0

Alright. I haven't really done anything such like what you are wanting to do.
But, my understanding is, for day one you would want to print 31..
Here is what I did to refactor it:

#include<stdio.h>

int main() {

    int j;
    int days=1;
    int value[]={31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
    
    for(days=1; days<=31; days++){
        
            for(j=1; j<=value[days]; j++){
                printf("%d \t %d\n", days, j);
            }
        
        
        }
}

It is kinda hard to figure a day as a number when you are giving the for loop 0-31...

I hope this somewhat helps you out..

0

As your data size is pretty large, I would suggest using a for loop to generate each day and then store them to an array as the for loop runs. You can print each day at the end of each loop. Here is how you can do it differently:

#include<stdio.h>

int main()
{
    int dayCount;
    int days = 31; //if you already knew you want to print days 1 - 31, might as well initiate the array size

    int value[days];
    int arrayCount;

    //assigning days 1 - 31 into the array
    for(dayCount = 1 ; dayCount <= 31 ; dayCount++)
    {
        value[dayCount] = dayCount;
        //you can also choose to print the element of the array here as the loop runs
    }

    //to print all elements of the array
    for(arrayCount = 1 ; arrayCount < days ; arrayCount++)
    {
        printf("Index: %d \tDay: %d\n", arrayCount, value[arrayCount]);
    }
}

However, this is only enough if you want to be rigid and strictly print days 1 - 31. If you have robustness in mind, best check out the answer from "RobertS supports Monica Cellio".