Home > Net >  Why does my for loop not loop the number of times I specified?
Why does my for loop not loop the number of times I specified?

Time:11-07

I have been trying to write a program in C but I don't know if it's correct because my for loop at the start ends after only 3 loops when it should end after 10. If someone could tell me where the issue is, I would appreciate it. Here is the program.

#include <stdio.h>
#define size 10
int main(int argc, char *argv[]){
    int array[size],sum[size],k,sum1,i,j;
    sum1=0;

    printf("give 10 integers:\n");
    for(i=0; i<size; i  ){
        scanf("%d", array[i]);
        sum[i]=0;
        }
    
    for(i=1; i<size; i  )
       {
        for(j=10; j>i; j--){
            if(array[j-1]>array[j]){
                k=array[j-1];
                array[j-1]=array[j];
                array[j]=array[j-1];

            }
        }
    }
    for(i=1; i<size; i  ){
            if(array[i]=array[i 1])
            sum[i]=sum[i] 1;
            }
    for(i=1; i<size; i  ){
            sum1=sum1 sum[i];
            }

 printf("%d", sum1);
}

I've tried deleting the other loops but then the for loops only once.

CodePudding user response:

The problem here is that your loop has to start from 0. Use:

for(i = 0; i < size; i  ) {
    ...
}

See, the loop will begin with i = 0 and will keep running so long as i < size. When i is exactly equal to size, it will not run anymore.

To get an idea of this, run the code:

#include <stdio.h>

int main() {
    int i, size = 10;

    for(i = 0; i < size; i  ) {
        printf("The value of i is: %d\n", i);
    }

    return 0;
}

This will show you that the loop never runs for i = 10, since when i is 10 then i < size is False, so the loop condition is false.

CodePudding user response:

Invalid scanf 2nd argument:

scanf("%d", array[i]);

array[i] is an integer, scanf requires a pointer, so:

scanf("%d", & array[i]);
// or
scanf("%d", array   i);

Affectation in condition:

if (array[i] = array[i 1])

Should be

if (array[i] == array[i 1])

Variable k is set, but never used.

Array 'sum' can be filled with 0s at declaration.

int sum[size] = { 0 };

Iterating array from index 1, instead of 0.

Index 'j' initialized with 10, while max index is 9.

Setting a variable to itself, did you mean to swap the variables using 'k' ?

array[j - 1] = array[j]; // both cells now have same value
array[j] = array[j - 1]; // so this means array[j] = array[j]

Improved the code a bit, but can't name other loops, I don't know what you were trying to achieve. Code compiles without error, only semantic errors are left.

#include <stdio.h>
#include <stdlib.h>

#define NUMBERS_COUNT 10

static void promptNumbers(int numbers[], int numbersCount)
{
    int readNumbers;

    printf("Give %d integers:\n", numbersCount);

    for (readNumbers = 0; readNumbers < numbersCount; readNumbers  )
        scanf("%d", numbers   readNumbers);
}

int main(void)
{
    int numbers[NUMBERS_COUNT];
    int sum[NUMBERS_COUNT] = { 0 };
    int i, j, sum1;

    sum1 = 0;

    promptNumbers(numbers, NUMBERS_COUNT);

    for (i = 0; i < NUMBERS_COUNT; i  )
    {
        for (j = 9; j > i; j--)
        {
            if (numbers[j - 1] > numbers[j])
            {
                numbers[j - 1] = numbers[j];
                numbers[j] = numbers[j - 1];
            }
        }
    }

    for (i = 0; i < NUMBERS_COUNT - 1; i  )
    {
        if (numbers[i] == numbers[i   1])
            sum[i] = sum[i]   1;
    }

    for (i = 0; i < NUMBERS_COUNT; i  )
        sum1 = sum1   sum[i];

    printf("%d", sum1);

    return EXIT_SUCCESS;
}
gcc -ansi -pedantic -Wall -Wextra -Werror temp.c -o temp
echo '1 2 3 4 5 6 7 8 9 10' | ./temp

EDIT: 2nd iteration, still unclear what you're doing with the array 'sum', counting how many duplicates you have ?

#include <stdio.h>
#include <stdlib.h>

#define NUMBERS_COUNT 10

static void promptNumbers(int numbers[], int numbersCount)
{
    int readNumbers;

    printf("Give %d integers:\n", numbersCount);

    for (readNumbers = 0; readNumbers < numbersCount; readNumbers  )
        scanf("%d", numbers   readNumbers);
}

static void swapNumbers(int * const first, int * const second)
{
    int swapBuffer = * first;
    * first = * second;
    * second = swapBuffer;
}

static void sortNumbers(int numbers[], int numbersCount)
{
    int i, j;

    for (i = 0; i < numbersCount; i  )
    {
        for (j = 9; j > i; j--)
        {
            if (numbers[j - 1] > numbers[j])
                swapNumbers(& numbers[j], & numbers[j - 1]);
        }
    }
}

int main(void)
{
    int numbers[NUMBERS_COUNT];
    int sum[NUMBERS_COUNT] = { 0 };
    int i, sum1;

    sum1 = 0;

    promptNumbers(numbers, NUMBERS_COUNT);
    sortNumbers(numbers, NUMBERS_COUNT);

    for (i = 0; i < NUMBERS_COUNT; i  )
    {
        if (numbers[i] == numbers[i   1])
            sum[i] = sum[i]   1;
    }

    for (i = 0; i < NUMBERS_COUNT; i  )
        sum1 = sum1   sum[i];

    printf("%d", sum1);

    return EXIT_SUCCESS;
}
gcc -ansi -pedantic -Wall -Wextra -Werror temp.c -o temp
echo '10 2 3 7 1 1 8 8 8 8' | ./temp
echo '10 2 3 7 4 1 9 6 5 8' | ./temp
  • Related