Volume of Rectangular problem

IN the following code i have taken input and after that i will take the same number of length,width and height of rectangular. But If I take n=3 then the number 1st and last shows right. but the others shows false..what can be edited???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<stdio.h>
main()
{
int i,n;
scanf("%d",&n);
int h,l,w;
int v[]={0};
for(i=0;i<n;i++)
{
scanf("%d%d%d",&h,&l,&w);
v[i]=h*l*w;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d",v[i]);
printf("\n");

}
}
Line 7 creates an array of size 1.
you have meant that i have to initialize the array to 1?
No I mean that the size of the array is too small if n > 1. v[0] is fine. v[1] is out of bounds.
the output if i initialize

1
2
3
4
 

int v[] ={1};



input: 3
1 2 3
2 3 1
5 6 1

output:
6
1
30 

The problem is when i > 1 and you do v[i] this will access memory outside the array, so you might actually be overwriting memory that belongs to other variables. This might cause problems or it might just work. You shouldn't rely on luck so better fix the problem.
oh i got your meaning.. But I am new .... so i don't get the right code to be edite.. please help me.. what should i use?
If you know std::vector you could use that.

If you know how to allocate the array dynamically you could allocate the array to have exactly n elements.

Otherwise you could set the size of the array to something big and give an error message if the user enters an n that is larger than the array size.
sorry i don't know.. :( can you pls make this program right?
you could say that n cannot be bigger than for example 50
and use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include<stdio.h>
#define N 50
main()
{
        int i,n;
        scanf("%d",&n);
        while(n>0&&n<=N)
        {
                printf("please give a number between 1 and 50
                scanf("%d",&n);
        }
        int h,l,w;
        int v[N]={0};
        for(i=0;i<n;i++)
        {
                scanf("%d%d%d",&h,&l,&w);
                v[i]=h*l*w;
        }
        printf("\n");
        for(i=0;i<n;i++)
        {
                printf("%d",v[i]);
                printf("\n");
        }
} 

because as I see you are writing C not C++
however if you know about malloc you could use it to dynamically allocate v
thanks Jewel.. i have made this using dynamic array.. i am quite new in programming however i learnt it long ago.. but no practice after that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include<stdio.h>
main()
{
int i,n;
int *v;
scanf("%d",&n);
int h,l,w;
v= new int[n];

v[n]=0;

for(i=0;i<n;i++)
{
scanf("%d%d%d",&h,&l,&w);
v[i]=h*l*w;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%d",v[i]);


printf("\n");

}
delete [] v;
return 0;
}


i ahve not learnt malloc..
Wait, if that's C code, why are you using new and delete?
And if that's C++ code, why are you using scanf, printf, #include <stdio.h> (instead of at the very least #include <cstdio> ), declaring main as main() instead of int main(), and declaring all of your variables at the beginning of the scope instead of when you need them?

Quick fix (assuming C):
-- Add #include <stdlib.h>
-- Change v= new int[n]; to v = (int*)malloc(n*sizeof(int));
-- Change delete [] v; to free(v);.
(I think -- I'm not a C programmer, so....)
oh... thanks.. but it worked on my complier..


OK.. i am nothing.. i just know little basicC++ and before that C :D after C++ leartn Assembly language ...:D
that's C code..
Topic archived. No new replies allowed.