AllExperts > C 
Search      
C
Volunteer
Answers to thousands of questions
 Home · More C Questions · Answer Library  · Encyclopedia ·
More C Answers
Question Library

Ask a question about C
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Prince M. Premnath
Expertise

I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience
  • Experience in this area :
  • Research over 4+ years.
  • Education/Credentials :
  • Masters in Computer Applications
  • Organization belongs to :
  • ST Joseph College (SFS) Trichy

 
   

You are here:  Experts > Computing/Technology > C/C++ > C > about inputing data into a 2x2 array

C - about inputing data into a 2x2 array


Expert: Prince M. Premnath - 5/13/2008

Question
Hi prince,

good to see you around.

Pls, blow is an array

array[5][2]= {
              1, 14,
               2, 28,
               3, 34,
               4,87,
                5, 20,
               };

I need you to please, teach me supposing i want to input these data into an array[5][2] without typing them in but using a simple code that asks me to input them. say this one:

int n=0;
int next;
while(scanf("%d", &next)==2){
 array[][2]= next;
}
will this programme help me to keep inputing the values i need i mean exactly entering datas into the array by me being prompted?

Also, i recently read about the insertion sort algorithm.

sort(array, n , value)

what value am in supposed to enter into the sort, since i want to sort an entire array. using the array declared blow pls help me out by trying the insertion sort on the array.

int array[5]= {3,8,1,4,9};

here is the insertion code:
sort(int array[], int n, int value){
int pos;
for(pos=n; pos>0&&value<array[pos-1]; pos--)
{
 [pos]= [pos-1];
 [pos] = value;
}

pls write a small code that will use the insertion sort method to sort the array for me pls. i want to see how to use this insertion sort cuz am so confused about the value parameter.

can i also sort a 2x2 array? if yes, pls a small very minute code pls.use the shell sorting method pls.i just need to know how to use the various sorting method i have learnt. so far, the one i can properly use is the bubble sort.

Hope you will help me pls.

Henry

Answer
HI Dear Henry !
Nice to meet you back with your question !

Of course its simple to read input to an two dimensional array lemme give you a code excerpt , will absolutely fit to the given two dimensional  array

for( i = 0 ; i < 2 ; i++)
{
  for ( j = 0 ;j < 4 ; j++)
  {
    scanf("%d" , &a[i][j]);
  }
}

Presenting you a simple code to perform insertion sort !
void insertionSort(int numbers[], int array_size)
{
 int i, j, index;

 for (i=1; i < array_size; i++)
 {
   index = numbers[i];
   j = i;
   while ((j > 0) && (numbers[j-1] > index))
   {
     numbers[j] = numbers[j-1];
     j = j - 1;
   }
   numbers[j] = index;
 }
}

We can sort a two dimensional , but we should be clear that the array should be sorted in row major order or in column major order besides memorizing the algorithm its really meaningful to understand the algorithm

Post a follow up for more quires !

With thanks and regards !
Prince M. Premnath.  

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.