user12887858
user12887858

Reputation:

How to resolve this segmentation fault inside my do-while loop?

You have to transport a maximum of 15 different loads from one port to another. Carrying capacity of a cargo ship, which will transport those loads, is 50 tons. Loads are enumerated and the information about the weight of every load is given as input.

Suppose that the weight of every load is smaller than or equal to 50 tons and greater than 0.

You will read the weight of every load from the input in a single line. Your input will end with a -1. You will print the number of trips necessary.

Sample Input:

50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 -1

Output:

15  

But I get this error and whenever I give input to program

./vpl_execution: line 5: 15093 Bus error               (core dumped) ./main

By the way I seached about this situation, I think I am not out of array index or wrong using wrong pointers. Also, I saw same question in here with some solution but I want to know why code is not working and then solve the question on my own. Thank you so much for your help, all.

#include <stdio.h>
int main()
{   int w,i,t,sum,index;
    int list[16];
    w = 1;
    do
    {   
        scanf("%d",&w);
        list[index] = w;
        index++;
    }while(w >= 0);
    t = 0;
    for(i = 0;i < ((sizeof(list)/sizeof(list[0]))+1);i++)
    {
        sum =0;
        if(sum <= 50)
        {sum += list[i];}
        else
        {t++;}
    }

    printf("%d",t);
    return 0;
}

Upvotes: 1

Views: 207

Answers (2)

Ardent Coder
Ardent Coder

Reputation: 3995

I get a segmentation error on my do while loop

The first segmentation fault happens at this line:

list[index] = w;

What do you think is the value of index?

You did not initialize it with a value, which probably should have been 0.

Therefore, accessing list[index] is undefined behaviour. In your case, it caused a segmentation fault.

Then inside for(i = 0;i < ((sizeof(list)/sizeof(list[0]))+1);i++)

Accessing list[i] here can cause another segfault at the final value of i. You should remove the +1 from ((sizeof(list)/sizeof(list[0]))+1).

Solution:

  1. Do index = 0; once (initialization) before doing list[index] = w;

  2. Remove the +1 from ((sizeof(list)/sizeof(list[0]))+1)

But it would be better to change

for(i = 0;i < ((sizeof(list)/sizeof(list[0])));i++) to

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

Because you already know the size of your list and list[15] is guaranteed to be -1 if there are 15 weights. So, you just need to traverse upto list[14].

This just removes the segfault, but there are still other problems in your code.

I want to know why code is not working and then solve the question on my own.

The logic inside your for-loop is wrong.

sum =0;
if(sum <= 50)

This condition is always true, and your else block which increments the value of t is never executed. Therefore, the output is always the initial value which you assign to t.

Upvotes: 1

hanie
hanie

Reputation: 1885

first you have used an uninitialized variable index use index=0.

also as question said a maximum of 15 different , but you are using ((sizeof(list)/sizeof(list[0]))+1) which aside from +1 at the end of it ,which will cause passing boundaries of your array which will lead to undefined behavior, is wrong , because you will traverse array completely while it's possible that you have less elements in array. you should add a counter while you are scanning data and your second loop should be based on that.

also your code is different from question , if input is always 0 < input <= 50 you will never enter else statement(t++) and you will always print 0. you are finding sum of weights and not count of them , and with the input explained in question you will always print t=0.

Upvotes: 0

Related Questions