Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
September 25, 2017, 04:36:47 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: program 7.1 little my problem about array  (Read 9316 times)
slackie
Newbie
*
Posts: 2


Email
« on: February 16, 2015, 10:28:02 PM »

hello everbody, i am newbie so please help me

i have a little problem about array in program 7.1 on book programming in c third edition

this is the program

// working with array

#include <stdio.h>

int main (void)
{

   int values[10];
   int index;

   values[0] = 197;
   values[2] = -100;
   values[5] = 350;
   values[3] = values[0] + values[5];
   values[9] = values[5] / 10;
   --values[2];

   
   for ( index =0; index <10; ++index)
   printf("values[%i] = %i\n", index, values[index]);
   
   return 0;

}


the result  should be like this

values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35


but my result is different

when is use mingw the result is

values[0] = 197
values[1] = 3
values[2] = -101
values[3] = 547
values[4] = 4218880
values[5] = 350
values[6] = 4202350
values[7] = 4202256
values[8] = 0
values[9] = 35

when i use cygwin the result is

values[0] = 197
values[1] = 1629192174
values[2] = -101
values[3] = 547
values[4] = 1629192174
values[5] = 350
values[6] = 1627419952
values[7] = 47
values[8] = 2280836
values[9] = 35

and  in cygwin when i ask program to give number again the result diffrent with before
why my result is different?
i have been  give value to index like this
for ( index =0; index <10; ++index)
index = 0; ( in for)
why the program like i don't use array
it is like i use ++index in values[index]
can you explain that sir
may be i  wrong about purpose the program

thank you Smiley
« Last Edit: February 17, 2015, 12:25:37 AM by slackie » Logged
slackie
Newbie
*
Posts: 2


Email
« Reply #1 on: February 17, 2015, 01:25:31 AM »

i heve been make program so the result like this

values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

this is the program

// working with array

#include <stdio.h>

int main (void)
{

   int values[10];
   int index;
   int i;   

   for ( i =0; i<10; ++i)
   values = 0;

   values[0] = 197;
   values[2] = -100;
   values[5] = 350;
   values[3] = values[0] + values[5];
   values[9] = values[5] / 10;
   --values[2];
   
   
   
   for ( index =0; index <10; ++index)
   
   printf("values[%i] = %i\n", index, values[index]);
   

   return 0;



}


but i corious why program in post 1 have result like that
can you explain
thank you
Smiley
Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
« Reply #2 on: February 17, 2015, 07:54:05 AM »

I'm not on Windows and I am unfamiliar with mingw and cygwin so I can not be certain but it seems to me that the runtimes for those systems are not initializing the arrays with zeros when they are declared. What you are seeing in the uninitialized array entries is whatever happened to be in that memory location when the printf statement is executed. This also would explain why the un-initied values change from one run to the next.

I'm seeing the same thing when I run a similar program in Xcode.
« Last Edit: February 17, 2015, 08:02:05 AM by BrianLawson » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.