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?!

Recommended Answers

All 6 Replies

Are you allowed to use vectors?

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

No, those are pointers. :)
Vectors are a kind of array that can grow and shrink very easy. An example:

#include <iostream>
#include <vector>
int main ()
{
    std::vector<int> my_buffer;
    my_buffer.push_back(2);
    my_buffer.push_back(5);

    std::cout << "My_buffer has " << my_buffer.size() << " ints in it";
    std::cout << "The first number is " << my_buffer[0];
    std::cin.get();
    return 0;
}

nope i need use pointers :icon_rolleyes:

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.

Your top array (the one containing arrays) should be defined something like (assuming you are storing ints): [B]int[/B][] 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:

#include <stdio.h>
#include <stdlib.h>

int main()
  {
  int[] lists[ 10 ];
  int   i, j;

  int *make_int_subarray( int length );

  // Initialize
  for (i = 0; i < 10; i++)
    lists[i] = NULL;

  // Fill some of the sub-arrays
  for (i = 0; i < 6; i++)
    lists[i] = make_int_subarray( (i+1) * 2 );

  // Print the arrays
  puts( "The arrays are:" );
  for (i = 0; i < 10; i++)
    {
    for (j = 1; j < lists[i][0]; j++)
      printf( "%i ", &lists[i][j] );
    printf( "\n" );
    }

  // Free all memory
  for (i = 0; i < 10; i++)
    if (lists[i] != NULL)
      {
      free( lists[i] );
      lists[i] = NULL;
      }

  return 0;
  }

int *make_int_subarray( int length )
  {
  int i;
  // Allocate storage for the array
  int *result = (int *)malloc( (length+1) * sizeof(int) );

  // Store its length
  result[0] = length;

  // Fill it with numbers: 1, 2, 3, ...
  for (i = 1; i <= length; i++)
    result[i] = i;

  return result;
  }

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.