Why is 'i' so big? I need it to be 1,2,3... - #include <stdio.h>
-
int
-
main()
-
{
-
int i,n,p;
-
printf("How many numbers:");
-
scanf("%d",&n);
-
int v[n];
-
for(i=1;i<=n;i++)
-
{
-
printf("n%d="),i;
-
scanf("%d",&v[i]);
-
}
-
printf("N=%d\n\n",n);
-
system("PAUSE");
-
}
-
Have you followed the advice given by Dheeraj? I think that would have fixed your problem.
Line 11 of your program is:
Notice that ,i is outside the parentheses. This means that i is not passed to printf. Instead, printf prints whatever garbage happens to be on the stack. When printf returns the compiler runs into the comma operator, which is effectively a semicolon. Then the compiler finds a statement consisting of a bare i; this is effectively a null statement (nop). Perhaps you are used to cout (in C++); printf works differently.
Another issue is that this printf is inside the loop and it doesn't emit a newline. This means that if the code were otherwise working and n were 5, the output would be "n1=n2=n3=n4=n5=" -- the concatenation of all the i values.
If you really want the output to be
n1=1
n2=2
...
then you want
but I don't know why you want to print the value of i twice.
By the way, when you declare an array as v[n] then the array index must be between 0 and n-1. Your index ranges between 1 and n. The last array access goes off the end of the array -- that's undefined behavior.
12 2248
I am not aware of such syntax.
Try
Regards
Dheeraj Joshi
If I remember correctly, doesn't C instantiated all variables at the beginning regardless of where it was declared? That's probably what's causing it.
to Dheeraj Joshi:
It prints the current number(n) and its value, example:
n1=1
n2=2
...
to Rabbit:
how can I fix it???
normally the value of i should be 1,2,3,...; if you test it you get big numbers, but the programs runs perfectly if you enter 3 it loops 3 times ?!?! I am confused ?! Please test it if I'm not clear...
This shouldn't even compile.
n needs to be a const value here and already be initialized at compile time.
Instead, this works and makes also use of the C++ style functions, which are a lot easier to use and safer: - #include <iostream>
-
#include <vector>
-
-
int main()
-
{
-
int i,n,p;
-
std::vector<int> v;
-
printf("How many numbers:");
-
std::cin>>n;
-
for(i=1;i<=n;i++)
-
{
-
std::cout<<i<<"=";
-
std::cin>>p;
-
v.push_back(p);
-
}
-
std::cout<<"N="<<n<<std::endl;
-
-
// You might now do this:
-
std::cout<<v.at(0);
-
// to print the first element
-
}
Edit: Here's a c style version that works: -
-
#include <stdio.h>
-
int
-
main()
-
{
-
int i,n,p; // p is unused however
-
int* v;
-
printf("How many numbers:");
-
scanf("%d",&n);
-
v = (int*) malloc(n*sizeof(int));
-
for(i=0;i<n;i++)
-
{
-
printf("n%d=",i);
-
scanf("%d",&v[i]);
-
}
-
printf("N=%d\n\n",n);
-
free(v); // Don't forget this
-
}
to donbock:
the value printed by printf("%d",i) which should be 1,2,3..
is instead 528465523,2584654,21576854,....
Have you followed the advice given by Dheeraj? I think that would have fixed your problem.
Line 11 of your program is:
Notice that ,i is outside the parentheses. This means that i is not passed to printf. Instead, printf prints whatever garbage happens to be on the stack. When printf returns the compiler runs into the comma operator, which is effectively a semicolon. Then the compiler finds a statement consisting of a bare i; this is effectively a null statement (nop). Perhaps you are used to cout (in C++); printf works differently.
Another issue is that this printf is inside the loop and it doesn't emit a newline. This means that if the code were otherwise working and n were 5, the output would be "n1=n2=n3=n4=n5=" -- the concatenation of all the i values.
If you really want the output to be
n1=1
n2=2
...
then you want
but I don't know why you want to print the value of i twice.
By the way, when you declare an array as v[n] then the array index must be between 0 and n-1. Your index ranges between 1 and n. The last array access goes off the end of the array -- that's undefined behavior.
Hi, tdlr
In you C version of the code you have mentioned new operator. There is no new operator in C.
Regards
Dheeraj Joshi
@Dheeraj Joshi:
Of course, you're right. And of course I meant: - v = (int*) malloc(n*sizeof(int));
-
/*...*/
-
free(v)
@donbock
Finally a solution!(sorry for my late response)
You found my mistake thank you very much, and thanks everyone else for trying.
As for your other advices, they are not so true:
you state that <the output would be "n1=n2=n3=n4=n5=">
let me point to the next line - scanf
after each value given the program goes automatically to a new line.
The purpose of the loop was store numbers from the user into v[n], so I don't need to print the value of i 2 times.
I just want the user to know at which number he is (n2,n6, n99...). I think you got the point.
And yes i in v[i] must start at zero, I knew it but I was rushing.
The final version of the complete code: - // Shows the numbers appearing more than once in a
-
//given array
-
#include <stdio.h>
-
int
-
main()
-
{
-
int i,j,n,t=0,p=0;
-
printf("How many numbers do you have:");
-
scanf("%d",&n);
-
int v[n];
-
for(i=0;i<n;i++)
-
{
-
printf("\n n %d = ",i+1);
-
scanf("%d",&v[i]);
-
}
-
-
for(i=0;i<n-1;i++)
-
for(j=i+1;j<=n;j++)
-
if (v[i]>v[j])
-
{
-
t=v[i];
-
v[i]=v[j];
-
v[j]=t;
-
}
-
printf("\n This numbers occurs more than once: ");
-
for(i=1;i<n;i++)
-
{
-
if(v[i]==v[i-1])
-
if(v[i]!=v[i+1])
-
{
-
printf(" %d",v[i]);
-
p++;
-
}
-
}
-
if(p==0)
-
printf(" every number is unique.");
-
printf("\n\n");
-
system("PAUSE");
-
}
-
Tell me what you think!
Line 10. FYI, a C89 compiler would flag this line as an error. That version of the language doesn't allow declarations to appear below executable statements. That version of the compiler doesn't support run-time specification of array size. You might want to add a comment to announce this limitation. Better would be to use conditional compilation to execute a #error if compiled with the wrong compiler version.
Lines 13-14. I realize now that you expect the scanf characters to be echoed to stdout. I'm not familiar enough with scanf to know if that is a portable assumption. Perhaps one of the other experts can tell us.
Lines 18-23. j will count up to n. In that last iteration the references to v[j] go beyond the end of the array.
Line 29. When i==n-1, v[i+1] goes beyond the end of the array.
- 18. for(j=i+1;j<=n;j++)
-
-
26. for(i=1;i<n;i++)
-
if(i!=(n-1))
-
{
-
if((v[i]==v[i-1])&&(v[i]!=v[i+1]))
-
{
-
printf(" %d",v[i]);
-
p++;
-
}
-
}
-
else if(v[i]==v[i-1])
-
printf(" %d",v[i]);
should be fine now (scanf is like cin>> in c++)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve |
last post by:
Hi,
I'm getting some output by running a command using os.popen. I need to
parse the output and transform it in some sense so that it's 'DB
compatible', (i.e I need to store the output in a...
|
by: Jonathan |
last post by:
I am getting an error message that other people have gotten. I have
done some search and reading enough to see that this error isn't only
happening to me. However, I am not able to discern a...
|
by: Michael |
last post by:
How can I get X (or another value) from this string
"asdfsadfX",
basically i want to get what ever is in between the tags and
place them in a variable called $www , can anyone help?
|
by: annbb |
last post by:
I have a piece of code which resets all the control on a form ..but if
I also put an option group on the form and the relevant code in the
routine to reset it I get an error message saying that...
|
by: Andy Baxter |
last post by:
Is there a way to pass parameters to functions by reference instead of by
value in javascript?
i.e. if I have code like this:
function setPointer(pointer) {
pointer=new String("hello world");...
|
by: mika_ella258 |
last post by:
the output should be like this
Loop Program
--------------------
enter 1st integer:
enter 2nd integer:
enter 3rd integer:
enter 4th integer:
enter 5th integer:
|
by: gert |
last post by:
Can you replace this->value with *this.value in c ?
|
by: dp_pearce |
last post by:
I have some code that takes data from an Access database and processes
it into text files for another application. At the moment, I am using
a number of loops that are pretty slow. I am not a...
|
by: dave816 |
last post by:
Sorry for the Excel question in an Access forum...................I don't see an Excel forum and there's probably a reason for that but figured I'd give this a shot anyway. Again sorry, delete if...
|
by: Kristoffer |
last post by:
I can't comprehend that this loop isnt working in practise as in my head it makes perfect theoretical sense
while(fscanf(input, "%c", &c)==1)
{
i = 0;
while(c!=';')
{
printf("%c",...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |