Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Dynamic arrays + shell sorting method Posted by LampaFX on 3 Jun 2009 at 1:41 PM
Hello everybody.
Basically, what I want to know is, how would shell shorting method work in such program , cause I don't clearly understand the algorithm of this method..

The program:
#pragma argsused
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int i,j; //cikla mainigie
int *p; //norade uz masivu ar noradem
int n; //masiva elementu daudzuma mainigais
int temp; //temp mainigais, hvz?
int *garumi; //dinamiska masivo kolonnu,rindu skaita daudzuma masivs
int **matrica; //dinamiskais masivs

int main(){
n=-10;

while (n<0)
{
printf("How much rows??\n");
scanf("\n%d", &n);
if (n<0)
{
printf("The amount of rows must be > 0!");
getch();
clrscr();
}
}
garumi=(int*)calloc(n,sizeof(int));
matrica=(int**)calloc(n,sizeof(int*));
for (i=0;i<n;i++)
{
printf ("How much columns will %d. row have?\n", i+1);
scanf ("%d",&garumi[i]);
while (garumi[i]<0)
{
if (garumi[i]<0)
{
printf ("number of columns must be > 0!");
getch();
clrscr();
}
printf ("How much rows in %d. row??\n", i+1);
scanf ("%d",&garumi[i]);

}
for (j=0;j<garumi[i];j++)
{
printf("Enter [%d][%d] element: ", i+1,j+1);
scanf("%d",&garumi[i]);

}

}
getch();

}
and now I should sort it, but I don't know how to do it, if anyone can help me, thanks :)

I would also like to know how can I change the amount of columns in a row? like add/delete function? I'm so confused about all this.
Report
Re: Dynamic arrays + shell sorting method Posted by Lundin on 4 Jun 2009 at 6:22 AM
I don't know what you are doing here:

garumi=(int*)calloc(n,sizeof(int));
matrica=(int**)calloc(n,sizeof(int*));

but you are not allocating a 2D array. See this link:

http://c-faq.com/aryptr/dynmuldimary.html
Report
Re: Dynamic arrays + shell sorting method Posted by LampaFX on 4 Jun 2009 at 3:06 PM
it actually does allocate a 2d array

garumi is a 1d array of column and row count in the second array and matrica is the array itself... that's the basic idea of it, it all works so far, but i need to sort the array I have now, and change the column count in each row :)

int *garumi; //dinamiska masivo kolonnu,rindu skaita daudzuma masivs - comment states, array which consists of number of rows and columns in the dynamic array




int **matrica; //dinamiskais masivs - the comment states, the dynamic array



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.