473,320 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re-size array of integers

16
Hello !

C programmers... I need your help.
I have a question regarding ARRAYS !

How can I resize (zoom in/out by a factor of 0.5 to 2.5) an array(8X8) of (integers) without using malloc function.

Update:
What is the best way to scale a 2D image array? For instance, suppose I have an image of 1024 x 2048 bytes, with each byte being a pixel. Each pixel is a grayscale level from 0 to 255. I would like to be able to scale this image by an arbitrary factor and get a new image. So, if I scale the image by a factor of 0.68, I should get a new image of size 0.68*1024 x 0.68*2048. Some pixels will be collapsed onto each other. And, if I scale by a factor of say 3.15, I would get a larger image with pixels being duplicated. So, what's the best way to accomplish this?


waiting...
May 5 '14 #1
7 1877
donbock
2,426 Expert 2GB
Quoting from the Wikipedia article on Image Scaling, "Scaling is a non-trivial process that involves a trade-off between efficiency, smoothness and sharpness." I would add a fourth trade-off for ease of programming.

There is no universally acclaimed "best" way -- it depends on how you choose to balance the trade-offs. So ... how do you want to balance the trade-offs?

By the way, why don't you want to use malloc?
May 5 '14 #2
linda7
16
I don't understand what do you mean by balancing the trade-off !
We didn't study the functions malloc and alloc yet ! so is there a simple way to do it without using malloc ! if not please tell me how to do it using malloc function


Thanks
May 5 '14 #3
donbock
2,426 Expert 2GB
There are two basic strategies for memory allocation in C: static allocation (when you know the size when you write the software) and dynamic allocation (when your program calculates the size at run-time). Dynamic memory allocation is accomplished through the malloc and free functions. If you're not going to use them then your only alternative is static allocation.

The actual size of the output array can vary, but the maximum possible size does not. The input array size is always [8,8]. The largest possible output array is [20,20] (because 8*2.5=20). Declare a fixed size output array with the maximum possible size and then only use a corner of it.

My comment about trade-offs refers to how you want to balance efficiency, smoothness, sharpness, and ease of programming. The question is moot for an 8x8 input array.
May 6 '14 #4
linda7
16
Okay let's use static memory allocation, how can I write the code for that ?
May 6 '14 #5
donbock
2,426 Expert 2GB
Please refer to the forum rules regarding Posting Homework or Coursework Questions and Answers.

You need to take a stab at writing this program yourself. We can help if you get stuck and have a specific question about C syntax or how to deal with a specific error message.
May 6 '14 #6
linda7
16
I have some errors, could you please check it


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define size 4
  4. int main(void)
  5. {
  6.     int list[size][size],i,j; // a[20][20]; maximum size    int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
  7.     int* ptr = malloc(size * sizeof(int));   //&list;
  8.  
  9.     for(i = 0; i < size; i++){
  10.         for(j = 0; j < size; j++)
  11.         printf("%d%d\n",ptr[i][j]);    //ptr[i][j] = i;
  12.  
  13.     }
  14.     for(i = 0; i < size; i++){
  15.         for(j = 0; j < size; j++)
  16.         printf("%d%d\n", ptr[i][j]);
  17.     }
  18.     printf("----------------------------------------\n");
  19.  
  20.     ptr = realloc(ptr, 20 * sizeof(int));    //malloc(20);
  21.  
  22.     for(i = 0; i < 20; i++){
  23.         for(j = 0; j < size; j++)
  24.           printf("%d%d\n", ptr[i][j]); // ptr[i][j] = i;
  25.     }
  26.     for(i = 0; i < 20; i++){
  27.         for(j = 0; j < size; j++)
  28.         printf("%d%d\n", ptr[i][j]);
  29.     }
  30.         free(ptr);
  31. }
May 7 '14 #7
donbock
2,426 Expert 2GB
You are using malloc, realloc, and free. Those functions are for dynamic memory allocation. I thought the plan was to use static memory allocation.

Your source code would have line numbers if you used code tags; that would make it easier to reference specific lines.
  1. You malloc enough memory for size (4) ints, but you want the array to hold 4*4=16 ints.
  2. In the first and third pairs of for loops you read from uninitialized *ptr locations (you commented out the code that would have initialized *ptr).
  3. In your 4 printf calls you have two %d's, but only pass one value to be printed.
  4. You realloc enough memory for 20 ints, but you want the array to hold 20*20=400 ints.
May 7 '14 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dean | last post by:
First I've must say Im completly new in php scripting What I need to do is upload, resize pictures with path in database Here is theory of it, and plan of doing it Hope somebody can help me...
16
by: Roland Hall | last post by:
In working with arrays, I have found that I am unable to dimension and array with a variable that has an integer value but I can redimension one this way. I haven't see any information that tells...
29
by: Chris Dutrow | last post by:
I searched around on the net for a bit, couldn't find anything though. I would like to find some code for a function where I input A Range Of Integers For example: Function( 1, 100 ); And the...
0
by: christopher diggins | last post by:
// big_int.hpp // The Diggins PDP (Public Domain Post) #3 // Public Domain code by Christopher Diggins, May 22, 2005 // // Description: // A naively implemented unsigned integer type for...
5
by: Simon Johnson | last post by:
In c# is it possible to resize an array cleanly? My application is that I have a list of numbers i'd like to return from a method as an integer array. I don't know before hand how many integers...
3
by: HateSpam | last post by:
I am defining a class that has, as a member, an array of another user-defined class. private mBoard as CBoardPosition() The problem comes when I attempt to size the array, it should be an 8x8...
12
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
8
by: er | last post by:
Hi All, I have an array x00,x01,x02,...,x0K0 x10,x11,x12,...,x1K1 .. .. .. xm0,xm1,xm2,...,xmKm
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.