It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
suppose i have a two D array.
#define ROWS 3
#define COLS 3
char a[ROWS][COLS]= {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
How can I copy values of that to a single array. I want only COLS values.
1) If you need to copy the array in row wise order
you could use :
-first have a 1D array that could hold all the elements of 2D array, then use
memcpy(new1Darr, org2Darr, total size in bytes);
like for above example,
memcpy(new1Darray, a, sizeof(char)*ROWS*COLS)
2) Instead of (1) or if you want to change the order in which the data must be stored then just traverse through the 2D array the way you want(column major) and copy the elements one by one. Like (considering you define all the variables first)
This will copy the elements in the new array in column wise order
k=0;
for(j=0;j<COLS;j++)
{
for(i=0;i<ROWS;i++)
{
new1Darray[k] = a[i][j];
k++;
}
}
Its very simple. Lets see 'how?'-
#define ROWS 3
#define COLS 3
#include<stdio.h>
#include<conio.h>
char a[ROWS*COLS]={'1','2','3','4','5','6','7','8','9'};
void main()
{
for(int i=0; i<ROWS*COLS;i=i+COLS)
{
for(int j=0; j<COLS; j++)
{
printf("%c\t",a[i+j]); //access array
}
printf("\n");
}
getch();
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
void main()
{
char name[5][10],temp[10];
int i,j;
for(i=0;i<5;i++)
{
printf("\nEnter the name of student:");
scanf("%s",name[i]);
}
for(i=0;i<(5-1);i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n the name of student is:");
for(i=0;i<5;i++)
{
printf("\n%s",name[i]);
}
getch();
}
I couldn't figure out the difference between insertion sort and selection sort ..Is this code following selection algorithm or Insertion ?
It is a specially slow version of selection sort.
It looks like a bubble sort, but a bubble sort would compare/swap the elements at positions j-1 and j, which are contiguous. You compare/swap the elements at positions i and j.
During every iteration of the outer loop, i remains constant while j advances from i+1 to the end. Thus, you end up having the element with the minimum value at position i.
You probably make a lot of unnecessary movements. A proper selection sort would search for the minimum value without moving anything. Then it would swap that minimum value with the value at position i. Therefore, it would perform only one swap per element in the array.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i have a function that generates randomize numbers between 0 and 2,and i want to use pointers but anyway i could not executed it.
First i generate randomize number and then i want to print the multidimensional array to see what it has generated
i just want to assign randomized number to an array(with using pointers) and then print them using pointers again
int **RandomizedMatrix; //integer multidimensional array
int ***AdressOfMatrix = &RandomizedMatrix; //assigning the adress of matrix to an integer pointer
void RandomizedMatrixGenerator(int LineNumber,int ColumnNumber,int ***AdressOfMatrix)
{
int i,j;
for (i=0; i<LineNumber*ColumnNumber; i++)
{
**AdressOfMatrix = rand()%2;
}
for (i=0; i<LineNumber; i++)
{
for (j=0; j<ColumnNumber; j++)
{
printf("%d",***(AdressOfMatrix+i)+j);
}
printf("\n");
}
}
If what i understand is right you want to fill your 2 dimensional array with 1s and 0s so you can do that
void randmtrgen(int row,int column,int **mtr){
int i,j;
for (i=0;i<row;i++){
for (j=0;j<column;j++){
mtr[row][column]=rand()%1+1;
}
}
}
That will fill your matrix as you wish and i suggest you to not use dereference operator if you are not good with it square brackets mostly fine.
And if you have an urge to use dereference operator you can use that line
*(*(mtr+row)+column)=rand()%1+1;
OK, so you want to assign random numbers you need two for() loops:
for (i=0; i<LineNumber*ColumnNumber; i++)
{
**AdressOfMatrix = rand()%2;
}
All this is going to do is assign LineNumber*ColumnNumber number of times the first element of your matrix a number. If you want to do it this way, you need to increment the rows and columns while assigning.
Also your "random" number isn't going to be random because you didn't seed it. Here's an example of what you wanted to do:
srand((unsigned) time(NULL)); // Seed the random number generator
for(row = 0; row<LineNumber; row++){
for(col = 0; col<ColumnNumber; col++){
ran = rand()%100; // Generate a randome numer 1-99
*(*(matrix+row)+col) = ran; // assign a random number to the element
}
}
for(row = 0; row<LineNumber; row++) {
for(col = 0; col<ColumnNumber; col++){
printf("[%d]", *(*(matrix+row)+col)); //Display the random number
}
printf("\n");
}
So given a 4x3 matrix, you could get an output like:
[99][33][38]
[59][2][28]
[50][39][2]
[85][45][99]
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was wondering how you would search through a 2D array (used as a parameter for a function), and find a specific character, e.g. an exclamation mark?
Say I have a 2D array island[20][40] and I want to find the character X. My approach would be to use a nested for loop, to go through each element and an if statement. E.g.
for (i = 0; i < 20; i++) {
for (j = 0; j < 40; j++) {
//Not sure what goes here (I want a function that identifies the element in the array)
if ((some variable) == 88)
printf("The treasure is at: (%d, %d)", i, j);
Thanks for your help :)
-island[20][40] works fine. I just want to know how to search through it for a specific character.
use the condition
if (island[i][j] == 88);
If your array(s) are not ordered then you have no choice but to search through sequentially, there are no short cut.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Does anyone know how to change the code below from enthusiasticgeek to allow for user input (e.g. Input n, and then n integers)? Output these n integers and the median. n is odd and positive and is less than 1 million.
Code pasted from enthusiasticgeek.com:
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 6
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, ELEMENTS, sizeof(int), compare);
for (n=0; n<ELEMENTS; n++)
{ printf ("%d ",values[n]); }
printf ("median=%d ",values[ELEMENTS/2]);
return 0;
}
use:
int values[static or dynamic allocation dependent]
for (i=0;i<n;i++){
printf("Enter number:");
scanf(%d, value[i]);
}
instead of just hardcoding the values for the values array.
Make sure you either dynamically declare the array, or statically make it large enough to accept the number of integers you want, 1,000,000 but thats a ton of memory. No one's going to take that much time to enter that many numbers manually. Probably safe with declaring it at 30 tops.
Or if you're planning to use the "define elements" part, you only need to declare the values array to be 6.