Sign in to follow this  
Followers 0
justsum13lse

Arrays in C

4 posts in this topic

I am pretty new to programming.  I am taking my first CS class at college.  I am having trouble understanding arrays.  I am having trouble envisioning them.  I have looked on the internet and can't really find anything that will help me.

I am having trouble understanding the concept of creating something like array[ j ]

I am making something like

 

int main(void)

{

int array[5];

int i, j;

for(i=0;i<5;i++)

{

printf("Row %d: ", i+1);

for(j=0;j<5;j++)                /* I understand that this lets me enter 5 numbers */

                 {

                 scanf("%d", &array[ j ]);      /* Does this now mean that the array will be populated by j? */

                 }

}

Part of my homework is to make a 2 dimensional array that adds the rows and the columns.  I subscribe to chegg and have the answer below, but I am having a really hard time envisioning it and understanding it.  The rowsum = rowsum+array[j] and colsum=colsum[j]+array[j].

Above I am just trying to make a program that just add the rows up.  I understand the first for(i=0;i<5;i++) lets me enter 5 rows.  The for(j=0;j<5;j++) lets me enter 5 numbers in each row.  then the scanf("%d", &array[ j ]) populates the variable array with numbers from the variable j?

 

Below I think I understand it to the point of the for(i=0;i<5;rowsum=colsum=0,i++).  I assume that line means while i =0, and i < 5, rowsum of i equals colsum of i which is equal to 0, i++.  But I don't really understand that last part.  and I don't understand why they have the 2 following for statements after that line, or how the rowsum and colsum lines following those for statements work.  I don't understand how it is adding the individual numbers up.  I am having trouble with 1 dimensional arrays let alone 2 dimensional arrays.  If anyone can help me or direct me to a decent website that goes in depth into arrays, I would greatly appreciate it.  Thanks.

 

 

 

#include <stdio.h>

int main(void)

{
int i, j, array[5][5], rowsum[5],colsum[5];

for(i=0;i<5;i++)
    {
    printf("Enter row %d: ", i+1);
    for(j=0;j<5;j++)
        scanf("%d", &array[ i ][ j ]);
    }
    for(i=0;i<5;rowsum=colsum=0,i++);
    for(i=0;i<5;i++)
    for(j=0;j<5;j++)
    
    {
        rowsum[ i ]=rowsum[ i ]+array[ i ][ j ];
        colsum[ j ]=colsum[ j ]+array[ i ][ j ];
    }
    
    printf("\nRow Totals:    ");
        for(i=0;i<5;i++)
    printf("%d\t", rowsum);
    printf("\nColumn Totals: ");
        for(i=0;i<5;i++)
    printf("%d\t", colsum);
    printf("\n");
    
    return 0;
            


}

Share this post


Link to post
Share on other sites

And I am trying to figure out why this is returning me a value of 0.

 

#include <stdio.h>

int main(void)

{
int i;
float total[5]={0};
float average;
int score[5];

for(i=0;i<5;i++)
    {
    printf("enter a score: ");
    scanf("%d", &score);
    }
for(i=0;i<5;i++)
    {
    total=total+score;
    }
    
printf("Total = %d", total);

    
return 0;

}

Share this post


Link to post
Share on other sites
 scanf("%d", &array[ j ]);  /* Does this now mean that the array will be populated by j? */ 

This code allows you as you said previously said in your comments you to enter a value using scanf. I am not entirely sure how the syntax is but as I understand it:
 

First you grab the value you enter using "%d" then this data value is stored in the pointer array which you have declared after with the position "j". "j" is the actual array position that you're currently at in the loop. The array index always begins at 0, so if you're looping 5 times the array would look like this: array[0], array[1], array[2], array[3], array[4], but since you're using the loop the numbers are replaced with the variable "j" visually in the code (but not in the actual array).

 

I am not sure what "for(i=0;i<5;rowsum=colsum=0,i++); " does either, I've never seen a for-loop being written like that :P

 

 

Edit: Second post:

Do not declare total as an array, keep it a simple int and give it a start value of 0. Then in the for-loop you must tell which index you want to grab the value of

total += score[i];

 

Share this post


Link to post
Share on other sites
41 minutes ago, justsum13lse said:

And I am trying to figure out why this is returning me a value of 0.

 

#include <stdio.h>

int main(void)

{
int i;
float total[5]={0};
float average;
int score[5];

for(i=0;i<5;i++)
    {
    printf("enter a score: ");
    scanf("%d", &score);
    }
for(i=0;i<5;i++)
    {
    total=total+score;
    }
    
printf("Total = %d", total);

    
return 0;

}

float + int  you got float

so code should be

        int i;
        float total =0 ;
        float average;
        int score[5];
        for (i = 0; i<5; i++)
        {
            printf("enter a score: ");
            scanf("%d", &score);
        }
        for (i = 0; i<5; i++)
        {
            total  += score;
        }

        printf("Total = %f", total);


        return 0;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0