C programming introduction

WBahn

Joined Mar 31, 2012
29,976
We're somewhat back to the same issue of me having to guess what problem you are trying to solve. You are doing things my specs didn't tell you to do and not doing something mine did. I don't know if that was intentional or not.

Your indenting implies a code structure which does not match the code.

Run your program and try to double the final number. Are you able to do it?

What is the purpose of the statement on line 29? What is the value of j when that statement is executed?
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
We're somewhat back to the same issue of me having to guess what problem you are trying to solve. You are doing things my specs didn't tell you to do and not doing something mine did. I don't know if that was intentional or not.

Your indenting implies a code structure which does not match the code.

Run your program and try to double the final number. Are you able to do it?
mistake
Code:
    if (number >0 && number <=5)
What is the purpose of the statement on line 29? What is the value of j when that statement is executed?
you said to use loop so I used loop to store new array value
 

WBahn

Joined Mar 31, 2012
29,976
mistake
Code:
    if (number >0 && number <=5)
It's a common mistake -- known as a "one-off" error. You want to get in the habit of looking for them.

you said to use loop so I used loop to store new array value
I'm asking about line #29, the line before your final loop:

array[j]= array[number-1];

What is the value of j when that statement is executed? What is it you are trying to accomplish with that statement?
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
What is it you are trying to accomplish with that statement?
problems specifications
1) Write a program that asks the user for five integer values.
2) Print them out as a comma-separated list of values on one line.
3) Print out the sum of the five values on the next line.
4) Ask the user which value they want to double, with 1 being the first value entered and 5 being the last value entered.
5) Print out the five values again.
6) Print out the sum of the five values on the next line.

look at this attempt

C:
#include<stdio.h>

#define clear  0

int main ()
{
    int n = 5, sum = 0;
    int i,  k, number, array[n];

    printf ( "Enter Value \n" ) ;

    for ( i = 0; i < n ; i++ )
       scanf ( "%d", &array[i] ) ;
       printf ( "Value : " ) ;

    for ( i = 0; i < n; i++ )
      {
        printf ( "%d", array[i] );

        if ( i == 5 - 1 )
            printf ( "\n" ) ;

        else
            printf ( ", " ) ;
      }

    for ( k = 0; k < n ; k++ )

        sum = sum + array[k];
        printf ( "sum : %d", sum );

    printf ( "\nEnter number which you want to double \n" );
    scanf ("%d", &number);

    if ( number > 0 && number <=5 )
 
        array[number-1] = array[number-1] * 2;
        printf ( "Double value : %d \n", array[number-1] ) ;

    printf ( "Value : " );

    for ( number = 0; number < n ; number++ )
        {
          printf( "%d", array[number] ) ;
     
          if ( number == 5 - 1 )
              printf ( "\n" ) ;
          else
              printf( ", " ) ;
        }

    sum = clear;

    for ( number = 0; number < n ; number++ )
         sum = sum + array[number];
         printf ( "sum : %d", sum ) ;
     
  return 0;
}
Enter Value
10
20
30
40
50
Value : 10, 20, 30, 40, 50
sum : 150
Enter number which you want to double
2
Double value : 40
Value : 10, 40, 30, 40, 50
sum : 170
 
Last edited:

WBahn

Joined Mar 31, 2012
29,976
Looks like you got it, other than the specs not saying anything about printing out the doubled value by itself.

Now try to clean up your code, particularly the indenting. For instance, humans will tend to see two indented lines under an if() statement as being a block of code controlled by that if(), but only the first statement is actually controlled by the if() unless both statements are within curly braces.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Now try to clean up your code, particularly the indenting. For instance, humans will tend to see two indented lines under an if() statement as being a block of code controlled by that if(), but only the first statement is actually controlled by the if() unless both statements are within curly braces.
WBahn sir, I do not think I can make it much better. That was my best attempt to format code. I would like to see how do you format.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
WBahn sir, I appreciate the way you explain.The way you explain is very good. I learned many new things from you in this thread

If you think array topic has been completed, then I would like to start with pointer.

I understand how to declare and initialize pointer in c language.

Pointer is a variable that store the address of another variable.

But I feel I don't understand proper concept behind the use of pointer.

C:
#include <stdio.h>

int main()
{
    /*Declaration of integer variable*/
    int variable = 50;

    /*Declaration of integer pointer*/
    int *pointer;   
    /*Assigning address of variable*/
    pointer = & variable ;
 
    /*Print the value of variable */
    printf("value of variable: %d\n",variable);
 
    /*print address of variable */
    printf("address of variable : %p\n",&pointer);
  
   return 0;
}
value of variable: 50
address of variable : 0061FF28

pointer = & variable
pointer is storing address 0061FF28 where variable value 50 is stored

Will you help me to understand pointers in c language as you helped me with array ?
 

WBahn

Joined Mar 31, 2012
29,976
WBahn sir, I do not think I can make it much better. That was my best attempt to format code. I would like to see how do you format.
I showed examples of formatted code earlier. Here is how I would recommend formatting your most recent code.

C:
#include<stdio.h>

#define ARRAY_SIZE (5)

int main ()
{
   int i, sum;
   int array[ARRAY_SIZE];

   // 1) Write a program that asks the user for five integer values.

   printf ( "Enter Value \n" ) ;

   for ( i = 0; i < ARRAY_SIZE; i++ )
      scanf ( "%d", &array[i] ) ;

   // 2) Print them out as a comma-separated list of values on one line.

   printf ( "Value : " );
   for ( i = 0; i < ARRAY_SIZE; i++ )
   {
      printf ( "%d", array[i] );

      if ( ARRAY_SIZE - 1 == i )
         printf ( "\n" ) ;
      else
         printf ( ", " ) ;
   }

   // 3) Print out the sum of the five values on the next line.

   for ( sum = 0, i = 0; i < ARRAY_SIZE; i++ )
      sum += array[i];
   printf ( "sum : %d", sum );

   // 4) Ask the user which value they want to double, with 1 being the first value entered and 5 being the last value entered.

   printf ( "\nEnter number which you want to double \n" );
   scanf ("%d", &i);

    if ( (0 < i) && (i <= ARRAY_SIZE) )
      array[i-1] *= 2;
   // printf ( "Double value : %d \n", array[i-1] ) ; // Not in specification

   // 5) Print out the five values again.

   printf ( "Value : " );
   for ( i = 0; i < ARRAY_SIZE; i++ )
   {
      printf ( "%d", array[i] );

      if ( ARRAY_SIZE - 1 == i )
         printf ( "\n" ) ;
      else
         printf ( ", " ) ;
   }

   // 6) Print out the sum of the five values on the next line.

   printf ( "Value : " ) ;   for ( sum = 0, i = 0; i < ARRAY_SIZE; i++ )
      sum += array[i];
   printf ( "sum : %d", sum );

  return 0;
}
Notice how I included the steps that make up the program specification as comments in the code associated with the code segment that satisfies that specification. I normally do this up front. I write out the steps that I need my code to perform as comments and then develop the code in between those comments. This helps keep me focused on what part of the code needs to do exactly what and lessens the chance that something will get overlooked. This is in addition to documenting the code in a way that ties directly to the problem that the code is intended to solve.

Also note how I tied the array size to a #define constant and that the constant is all caps to keep it from being confused with a variable at some point.

As noted previously, the variables names i, j, k and similar are generally understood as being highly localized dummy variables. A common practice is to use 'i' as much as possible and to only use 'j' when 'i' is already being used at the same time and then to only use 'k' when both 'i' and 'j' are already in use at the same time. This is often the case with nested loops and multi-dimensional arrays. In this case, there's no need for anything other than 'i'.
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
Here is how I would recommend formatting your most recent code..
Excellent work, I could not even imagine that it could happen. I have seen many programs on internet but your style is awesome.I really like the way you write program I'll do my best to adopt your style of writing program
 

MrChips

Joined Oct 2, 2009
30,704
Also, it would not hurt if you got into the habit of always enclosing block code in curly braces, even if the block is a single line of code.
Code:
if ( )
{
   ... code
}

for ( )
{
   ... code
}
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Also, it would not hurt if you got into the habit of always enclosing block code in curly braces, even if the block is a single line of code.
Code:
if ( )
{
   ... code
}

for ( )
{
   ... code
}
Wbhan Sir has given different advice Now It is difficult for me, Whose advice should be considered.

Why do you think its good to use enclosing block for single statement?
I was doing this previously
One thing that can make your code more readable is to not use enclosing braces for single line blocks of code. But there is a downside in that if you later add a second line you may forget to add the now-mandatory braces. There are different schools of thought on this one..
 
Last edited:

MrChips

Joined Oct 2, 2009
30,704
One thing that can make your code more readable is to not use enclosing braces for single line blocks of code. But there is a downside in that if you later add a second line you may forget to add the now-mandatory braces. There are different schools of thought on this one.
Like the man says, there is a downside. There are different schools of thought.

I ways type ( followed by )
and { followed by }
and then fill in the code in the middle afterwards.

You will never forget the braces and will never go wrong by doing it this way.

For example,
Code:
if ( )
{
}
else
{
}
 

WBahn

Joined Mar 31, 2012
29,976
Wbhan Sir has given different advice Now It is difficult for me, Whose advice should be considered.

Why do you think its good to use enclosing block for single statement?
I was doing this previously
Note that I stated that there are pros and cons and that there are different schools of thought as to what the best approach is.

The downside to my recommendation was noted where I made it. The upside in comparison is that you can see more code on the screen at one time. If you write functions so that they can be viewed entirely it makes it a lot easier to get in the mindset of that function -- humans are good at keeping big-picture contexts when we can keep it in our visual field.

This is true with other style areas, as well. Perhaps the biggest one is the location of the opening curly brace on a block of code (i.e., a compound statement). The "K&R" style is to put the opening brace at the end of the line of code containing the controlling statement, while the Allman style puts it on the next line (this is the style you have been using). There are many others, but these two seem pretty dominant.

https://en.wikipedia.org/wiki/Indentation_style

The K&R is the older style and so a lot of people out there are simply used to it, while the Allman style has been gaining ground over the last few of decades. I strongly prefer the Allman style and almost equally dispise the K&R style. I think K&R wouldn't have used that style had they had today's display resources.

You need to decide what style works best for you and this is influenced not only by your personal preferences, but also what you will encounter in code written by others. Furthermore, you may not have a say as your employer may dictate a style that they expect you to use.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Alright! Can we come back to the post 47. I showed it in what I understand about pointer in c language

How to get grasp knowledge of pointer by solving problems as we were doing for array
 
Top