Array[count+1] legal?


 
Thread Tools Search this Thread
Top Forums Programming Array[count+1] legal?
# 1  
Old 12-11-2013
Array[count+1] legal?

I get weird decimal digits when I run the program below.

Code:
  int coe_amount;

     cout << "How many coefficients exist in your term? ";
     cin >> coe_amount;

     float coefficient[coe_amount];

     for (int count = 0; count < coe_amount; count ++) 
                {            
                     cout <<    "Enter the coefficients of your variables, for term " << count << ": ";
                    cin >> coefficient[count];
                }

     for (int count = 0; count < coe_amount; count ++) 
                    {
                        
                        cout << coefficient[count+1];

                        cout << coefficient[ count];
                    }

It might have something to do with coefficient[count+1], which I don't know if it is a legal statement.

Basically I want the "coefficient[count+1]" to print the list of array elements that is one subscript ahead of coefficient[ count], for
"count < coe_amount" times. Is this how you do it? If not can somehow show me the correct syntax?
# 2  
Old 12-11-2013
On the very last loop, [count+1] will go beyond the end of the array. So make your loop one shorter.

Also, this is not legal:

Code:
float coefficient[coe_amount];

...or rather, is legal only in a few very new and specific revisions of C/C++ which you might not have. Do this instead:
Code:
float *coefficient=new float[coe_amount];

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-11-2013
count+1 is legal but you should stay within the defined range 0...coe_amount-1.
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 12-12-2013
Quote:
Originally Posted by Corona688
On the very last loop, [count+1] will go beyond the end of the array. So make your loop one shorter.

Also, this is not legal:

Code:
float coefficient[coe_amount];

...or rather, is legal only in a few very new and specific revisions of C/C++ which you might not have. Do this instead:
Code:
float *coefficient=new float[coe_amount];

I haven't run into any trouble just doing:

Code:
int amount;

cin >> amount;

float number[amount];

I guess I am safe from doing this? It's more intuitive to me.
# 5  
Old 12-12-2013
Not really, no. Variables are supposed to have their size fixed at compile-time, not runtime. I've had to fix lots of code where people have done things like that. It happened to work for them by sheer coincidence but compile it on a different system and it crashes.

If you tell a C compiler to do something crazy, it will do so, and not always complain, but might not do what you actually wanted. Don't trust that code is "right" just because it "works".
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count number of unique values in each column of array

What is an efficient way of counting the number of unique values in a 400 column by 1000 row array and outputting the counts per column, assuming the unique values in the array are: A, B, C, D In other words the output should look like: Value COL1 COL2 COL3 A 50 51 52... (16 Replies)
Discussion started by: Geneanalyst
16 Replies

2. UNIX for Beginners Questions & Answers

Awk: count unique element of array

Hi, tab-separated input: blabla_1 A,B,C,C blabla_2 A,E,G blabla_3 R,Q,A,B,C,R,Q output: blabla_1 3 blabla_2 3 blabla_3 5 After splitting $2 in an array, I am trying to store the number of unique elements in a variable, but have some difficulties resetting the variable to 0 before... (6 Replies)
Discussion started by: beca123456
6 Replies

3. Shell Programming and Scripting

Array Count and Array Copy help

Hi all, I have the code as below " echo "File carried list after 1st loop "${fileStreamAssiagnInit}"" and I have the out put for the above code as below : Output : File carried list after 1st loop abcInd.csv sdgUS.csv sopSing.csv Here i want to count the number of elements in... (3 Replies)
Discussion started by: Balasankar
3 Replies

4. Shell Programming and Scripting

Beginner: Count & Sort Using Array's

Hi, I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help. I have a file (mainfile.txt) with comma deliminated values, like so: $1 $2 $3 613212, 36, 57 613212, 36, 10 613212, 36, 10 677774, 36, 57 619900, 10, 10 i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies

5. AIX

Legal Disclaimer setup in CDE

Hi pals I manage nearly 200+ aix workstations. I need to setup a legal disclaimer in all the workstations. When the user do a interactive login in CDE the legal disclaimer should be displayed and once he accepts the same he should be able to login to system. Can anybody suggest me as to... (0 Replies)
Discussion started by: sriram.s
0 Replies

6. Programming

legal code?

hi friends, the following code works fine,but the question is "is this a valid c". i really have no idea....... void func() { int x = 50; { int y; y = x + 400; printf("x = %d\n",x); printf("y = %d\n",y); } } (2 Replies)
Discussion started by: mxms755
2 Replies

7. Shell Programming and Scripting

Not Legal Characters

I have a file that I want to grep and identify all of the illegal characters. I have a list of legal ascii characters \11\12\40-\176,\0-\255 so i try a grep -v to exclude these but my syntax is not correct?? $ cat TRANS_20050613_00.DAT.ERROR | grep -v '\11\12\40-\176\0-\255' grep:... (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question