GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Nov-2017, 19:07
afzar afzar is offline
New Member
 
Join Date: Nov 2017
Posts: 1
afzar is on a distinguished road
Post

Help Understanding the Role of a Variable in an Array


Hi Guys,

The following code is from Sams Teach Yourself C:

CPP / C++ / C Code:

/* Lesson 8 expenses.c demonstrates use of an array */

#include <stdio.h>

/* Declare an array to hold expenses, and a counter variable */

float expenses[13];
int count;
float year_expenses = 0;

int main(void)
{
	/* Inpt data from keyboard into array */

	for(count = 1; count < 13; count++) 
	{
		printf("Enter expenses for month %d: \n", count); 
		scanf("%f", &expenses[count]);
	}

	/* Print array contents */

	for (count = 1; count < 13; count++)
	{
		printf("Month %d = $%.2f\n", count, expenses[count]);
		year_expenses += expenses[count];
	}
	printf("Yearly expenses are $%.2f\n", year_expenses);
	return (0);
}
// What role does the variable count play in this case: expenses[count].
  #2  
Old 13-Nov-2017, 14:59
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,516
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Help Understanding the Role of a Variable in an Array


Do you know about array indexing?

The variable count is used to refer to a specific array index.

So when the loop runs, which sets count from 1 to 12, means that the array positions will be indexed in this manner:
CPP / C++ / C Code:
    expenses[1] // The 1st array index position
    expenses[2] // The 2nd index
    // ...
    expenses[12] // The last array index
There is more that could be said about the way that array is used, but I'll stop here as it might be covered later in the chapter, or some future chapter in your book.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrays and Pointers swarna.bhatt C Programming Language 6 09-Apr-2013 01:23
Exception in thread "main" java.lang.NullPointerException at Cameras.sortByName(Came Dan0756 Java Forum 1 24-Aug-2012 19:35
How to get the amount of memory allocated from a pointer? cpit C Programming Language 2 11-Aug-2011 17:46
NetBeans - Inventory Program biglez123 Java Forum 1 23-Jan-2011 20:48
Variable Structure Array Help LexEtEos C Programming Language 5 22-Nov-2010 08:27

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:56.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.