Thread: using a for loop to initialize an array

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    using a for loop to initialize an array

    I can't seem to figure this out. How can I use a for loop to accomplish the following?
    Code:
    classAverage[0] =
    	(stuArray[0].grade[0] +
    	 stuArray[1].grade[0] +
    	 stuArray[2].grade[0] +
    	 stuArray[3].grade[0])/4;
    	classAverage[1] =
    	(stuArray[0].grade[1] +
    	 stuArray[1].grade[1] +
    	 stuArray[2].grade[1] +
    	 stuArray[3].grade[1])/4;
    	classAverage[2] =
    	(stuArray[0].grade[2] +
    	 stuArray[1].grade[2] +
    	 stuArray[2].grade[2] +
    	 stuArray[3].grade[2])/4;
    	classAverage[3] =
    	(stuArray[0].average +
    	 stuArray[1].average +
    	 stuArray[2].average +
    	 stuArray[3].average)/4;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't. You use two for loops (one for the slow-changing indices, one for the fast).

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by tabstop View Post
    You don't. You use two for loops (one for the slow-changing indices, one for the fast).
    Ahh ha!! Thanks!!! This worked for me...
    Code:
    int i;
    	int j;
    
    	for (i = 0; i < sizeof(stuArray); i++)
    	{
    	
    		for (j = 0; j < sizeof(stuArray); j++)
    			
    			(stuArray[i].grade[j] +
    			 stuArray[i].grade[j] +
    			 stuArray[i].grade[j] +
    			stuArray[i].average)/4;
    	}

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Really? I'm assuming you've left bits out, then, and changed some other bits.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by tabstop View Post
    Really? I'm assuming you've left bits out, then, and changed some other bits.
    LOL! I spoke too soon. I had forgot to comment out my old code. Uhhg!

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    I'm getting a syntax error saying "invalid operands to binary +"
    Code:
           float classAverage[4];
    	int i;
    	int j;
    
    	for (i = 0; i < sizeof(stuArray); i++)
    	{
    	
    		for (j = 0; j < sizeof(stuArray); j++)
    		{
    			classAverage = classAverage +
    			(stuArray[i].grade[j] + 
    			 stuArray[i].grade[j] + 
    			 stuArray[i].grade[j] +
    			 stuArray[i].average)/4;
    		}
    	}

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    stuArray[i].average is what? (I'm guessing that's the + that's being complained about.)

    Not that it matters, since it shouldn't be there in the first place, of course.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Quote Originally Posted by tabstop View Post
    stuArray[i].average is what? (I'm guessing that's the + that's being complained about.)

    Not that it matters, since it shouldn't be there in the first place, of course.
    it is supposed to average all the grades of each stuArray[i].grade.

    Maybe the rest of the code would help clarify things.
    http://www.box.net/shared/vtcg3h2mek

  9. #9
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Okay, I think I'm getting closer. My instructor said not to use sizeof(stuArray). But that wasn't my only problem to begin with. I think my problem lies with accessing the average member of the stuArray.
    Code:
    void D_printClassAverage(student* stuArray)
    {
    	float classAverage[4];
    #if 1
    	int i;
    	int j;
    
    	for (i = 0; i < CSIZE; i++)
    	{
    	
    		for (j = 0; j < CSIZE - 1; j++)
    		{
    			classAverage[i] = classAverage[i] + ((stuArray[i].grade[j])/ CSIZE);
    		}
    		classAverage[i] = classAverage[i] + (stuArray[i].average)/CSIZE;
    	}
    Last edited by MSF1981; 02-22-2009 at 11:35 PM. Reason: forgot code tags

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM