Thread: How to update value

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    52

    How to update value

    How to update array value in program

    Code:
    #include <stdio.h>
    int main ()
    {
       int i, input;
    
       int array[5] = { 10, 20, 30, 40, 50 };
       printf ("Enter input\n");
       scanf ("%d", &input);
    
      if (input > 0 && input < 5)
        // array value depend on input
        // input is 1 then 10 become 5
        // update array =  5, 20, 30, 40, 50
       // input is 3 then 30 become 15
        // update array =  5, 20, 15, 40, 50
        
        return 0
        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,690
    So have you figured out the relationship between 10 and 5, or between 30 and 15 ?

    What about the relationship between the number you input (say 3), and the subscript for the value you intend to modify?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    52
    Quote Originally Posted by Salem View Post
    So have you figured out the relationship between 10 and 5, or between 30 and 15 ?

    What about the relationship between the number you input (say 3), and the subscript for the value you intend to modify?
    new value is half of original value

    first list 10, 20, 30, 40, 50
    say input is 1

    first list 5, 20, 30, 40, 50

    say input is 4
    first list 5, 20, 30, 20, 50

    I don't understand how to update new value in array ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,690
    You mean you've never assigned a value to an array element before?

    array[i] = array[i] + 1;

    Now do what you want to do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    52
    Quote Originally Posted by Salem View Post
    You mean you've never assigned a value to an array element before?

    .


    five integer stored into array

    int array[5] = { 10, 20, 30, 40, 50 }

    Ask the user which value they want to half
    input number 1 to 5

    let say input is 2 : 20

    print the array with new value

    int array[5] = { 10, 10, 30, 40, 50 }

    where 10 is half value of 20



  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,450
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to update CListBox
    By rluceac in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2009, 07:58 AM
  2. Update LabelControl
    By Coding in forum C# Programming
    Replies: 2
    Last Post: 02-23-2008, 08:00 AM
  3. C++: How can I update a variable
    By nekkron in forum C++ Programming
    Replies: 21
    Last Post: 12-13-2007, 02:35 PM
  4. Connect4 update
    By Rutabega in forum Game Programming
    Replies: 1
    Last Post: 01-06-2004, 02:26 PM
  5. Update
    By Unimatrix_001 in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2003, 05:42 PM

Tags for this Thread