Insertion Sort Error in C++


 
Thread Tools Search this Thread
Top Forums Programming Insertion Sort Error in C++
# 1  
Old 04-12-2013
Insertion Sort Error in C++

TD P { margin-bottom: 0in; }P { margin-bottom: 0.08in; }TT.cjk { font-family: "WenQuanYi Zen Hei Sharp",monospace; }A:link { } I am trying to run this program for Insertion Sort, But don't know that why I am getting following Error

Code:
#include<iostream>
int main(){
int i,j,key,n;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++) scanf("%d",&a[i]);
for(j=1;j<n;j++){
key=a[j];
i=j-1;
while(i>=0&&a[i]>key){
a[i+1]=a[i];
i=i-1;
}
a[i+1]=key;
} 
for(j=0;j<n;j++) printf("%d ",a[j]);
return 0;
}

Code:
                                 Error
In function 'int main()':
Line 10: error:             ISO C++ forbids variable-size array 'a'
compilation terminated             due to -Wfatal-errors.


Last edited by Corona688; 04-12-2013 at 12:54 PM..
# 2  
Old 04-12-2013
It's telling you the problem: You're not allowed to make a variable size array like that. 'int a[5]' type arrays have to be fixed in size at compile time, you can't pick the size later.

Do this instead:

Code:
int *a=new int[n];

Which you can use just like an array afterwards.
# 3  
Old 05-02-2013
in fact, if you have the oppertunity to use C++11, it is a solution to this old problem. The new standard of C++ says variable size arrays are aloud, if you supply a certain keyword(i forgot), which indicates to the compiler that the programmer knows for sure the variable is not going to be 0 or negative value.
# 4  
Old 05-02-2013
...which will cause it to blow up or go bananas whenever you use a compiler that doesn't. I've had to "fix" several pieces of code like that.
# 5  
Old 05-03-2013
It is true that many compiler (or maybe safe to say none) do support the full standard of C++11. And many do not even have decent support for basic C++11 standard, so I guess we have to stick to the C++98 solution with new.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Simple, I think, VIM Insertion

Hey everyone, I recently had to learn how to use UNIX and VIM for an internship, and have run in to a little challenge today. I have lists of thousands of file names that represent CAS (Chemical Abstracts Service) numbers, but they are not formatted correctly. CAS numbers run in the form of... (2 Replies)
Discussion started by: Stuart Ness
2 Replies

2. Shell Programming and Scripting

Insertion into csv

I want to use bash to insert the variable $a into the cell A1 of a csv file mycsv.csv. How do I insert a variable into a specific cell in a csv file? (1 Reply)
Discussion started by: locoroco
1 Replies

3. Programming

Bus error in tree insertion

Hi, I am new to C++ and unix. I am trying to write a programm for inserting elements into a binary tree. To get the code flow I used few Couts and m facing buss error while insertion. Below is the code snippet. explainations needed. thanks :) #include <iostream.h> struct mytree { int... (1 Reply)
Discussion started by: vineetjoshi
1 Replies

4. Programming

C: Binary Insertion Sort Optimization: Scoreboard

Hello, As part of a scheme to determine the top 10 5x5 Boggle board configurations beyond a reasonable doubt, I set out to optimize every aspect of the problem, and this is part one of a series of web pages that I am going to publish shortly... Optimal Binary Insertion Sort - Scoreboard Is... (0 Replies)
Discussion started by: HeavyJ
0 Replies

5. Shell Programming and Scripting

Insertion in a file

Hi All, I want to insert a line just befor the lst line in a file. please can anyone advise. Cheers, Shazin (3 Replies)
Discussion started by: Shazin
3 Replies

6. Ubuntu

how can insertion of one module deletesother modules

Masters, I have inserted a linux module which controlls the bandwidth of LAN . Just when I insmod'ed kernel panicked. When restarted I am stunned to find out that many of my other modules are missing. How can insertion of one module removes other modules . I use 2.6.9 kernel on a redhat 3.4.3... (1 Reply)
Discussion started by: iamjayanth
1 Replies

7. Shell Programming and Scripting

Insertion of Header record

A header record is to be inserted in the begining of a flat file without using extra file or new file. It should be inserted into same file. Advace thanks for all help... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

8. UNIX for Dummies Questions & Answers

insertion sort???

Hi, I was wondering if someone here could help me figure out what's wrong with this simple insertion sort shell script. This is the output I get when I try to run it: "23 43 22 15 63 43 23 11 10 2 ./insertion.sh: line 23: 23 43 22 15 63 43 23 11 10 2 And here's the script: ... (2 Replies)
Discussion started by: sogpop
2 Replies

9. Shell Programming and Scripting

automatic header insertion

hi Is there any way to automatically insert a predefined header into a file? It would include the file name, author name, date and a description, the description entered on the first line of vi. Thanks for any help Oliver (4 Replies)
Discussion started by: olimiles
4 Replies

10. UNIX for Advanced & Expert Users

Insertion of Leap Second

Hi All, We are running the HP-UX 11.11 and Linux AS 3.0. so, shall we need to make any changes for leap second i.e. insert the leap second on 1st Jan 2006 or does the system have some setup which would take care of this automatically. Please advise. Regards, Inder (2 Replies)
Discussion started by: isingh786
2 Replies
Login or Register to Ask a Question