User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,778 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,340 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 272 | Replies: 6
Reply
Join Date: May 2008
Posts: 3
Reputation: Sephy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sephy's Avatar
Sephy Sephy is offline Offline
Newbie Poster

Row length changes?

  #1  
May 16th, 2008
So basically i got to make program where you can add rows, inside those rows numbers and count in each row and all rows pair number sum.

This far im done and everything is working. Now part which don't want work. I need make option to make rows longer or shorter. For example:

row1 contains 1,4,5,2,4
row2 contains 1,2,5

i want make row1 longer and add 6. row1 now will contain 1,4,5,2,6

so here is code i made so far:
case '3':{
clrscr();
row=0;
printf("\n Which row length you want change?");
flushall();
scanf("%hd",&row);
g=row-1;              //this is for going to row[0] element
elemcount=lengthie[g]; //this count how much elements i got it my row currently
printf("\n%hd.row  length now is %hd\n",g+1,lengthie[g]);
printf("For how much you want change row length??");
scanf("%hd",&change);
if (change=0){printf("Row length not changed");}

if (change<0){ newlenght=vecaisskaits-change;lengthie[g] = (short int) realloc(newlenght, sizeof(short int));}

if (change>0){ newlenght=elemcount+change;
lengthie[g] = (short int) realloc(newlenght, sizeof(short int));
for(q=elemcount;q=lengthie[g];q++){
printf("\n Insert %hd. row %d. element",g+1,q);
flushall();
scanf("%hd",&elements[g][q]);
}
}  */
getch();
break;}
any sugestions?!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Row length changes?

  #2  
May 16th, 2008
Are you allowed to use vectors?
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: Sephy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sephy's Avatar
Sephy Sephy is offline Offline
Newbie Poster

Re: Row length changes?

  #3  
May 16th, 2008
vectors are those thingies vs *? if yes then yes

btw they are used to reserve my row length because it can be different for each row
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Row length changes?

  #4  
May 16th, 2008
No, those are pointers.
Vectors are a kind of array that can grow and shrink very easy. An example:
  1. #include <iostream>
  2. #include <vector>
  3. int main ()
  4. {
  5. std::vector<int> my_buffer;
  6. my_buffer.push_back(2);
  7. my_buffer.push_back(5);
  8.  
  9. std::cout << "My_buffer has " << my_buffer.size() << " ints in it";
  10. std::cout << "The first number is " << my_buffer[0];
  11. std::cin.get();
  12. return 0;
  13. }
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: Sephy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sephy's Avatar
Sephy Sephy is offline Offline
Newbie Poster

Re: Row length changes?

  #5  
May 16th, 2008
nope i need use pointers
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,779
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Row length changes?

  #6  
May 16th, 2008
In that case, you have some serious problems

As my signature says '= != =='. You have = and == mixed up:
here: for(q=elemcount;q=lengthie[g];q++)
and here: if (change=0)

flushall(),clrscr(),and getch()
won't work on standard compilers only on your Turbo thing, so get rid of 'em. Use getchar() to pause the program.

And I haven't looked at your logic yet.
Since this is more a C then a C++ question, I'll ask a mod to move your thread.
Last edited by niek_e : May 16th, 2008 at 4:39 am.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Row length changes?

  #7  
May 16th, 2008
Your top array (the one containing arrays) should be defined something like (assuming you are storing ints):
int[] lists[ 10 ]; // Ten arrays of variable length.

Now you must decide how to know how long each array is. You could store its length as the first integer in the array. For example:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int[] lists[ 10 ];
  7. int i, j;
  8.  
  9. int *make_int_subarray( int length );
  10.  
  11. // Initialize
  12. for (i = 0; i < 10; i++)
  13. lists[i] = NULL;
  14.  
  15. // Fill some of the sub-arrays
  16. for (i = 0; i < 6; i++)
  17. lists[i] = make_int_subarray( (i+1) * 2 );
  18.  
  19. // Print the arrays
  20. puts( "The arrays are:" );
  21. for (i = 0; i < 10; i++)
  22. {
  23. for (j = 1; j < lists[i][0]; j++)
  24. printf( "%i ", &lists[i][j] );
  25. printf( "\n" );
  26. }
  27.  
  28. // Free all memory
  29. for (i = 0; i < 10; i++)
  30. if (lists[i] != NULL)
  31. {
  32. free( lists[i] );
  33. lists[i] = NULL;
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39. int *make_int_subarray( int length )
  40. {
  41. int i;
  42. // Allocate storage for the array
  43. int *result = (int *)malloc( (length+1) * sizeof(int) );
  44.  
  45. // Store its length
  46. result[0] = length;
  47.  
  48. // Fill it with numbers: 1, 2, 3, ...
  49. for (i = 1; i <= length; i++)
  50. result[i] = i;
  51.  
  52. return result;
  53. }

Hope this helps.
Last edited by Duoas : May 16th, 2008 at 9:27 pm. Reason: Made it compilable.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 2:52 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC