A for loop in C using an array - c

I've created an array named a that can hold 100 double values,
double a[100];
I set the first element of the array a to NUM, which is a symbolic constant defined early in my code.
a[0] = NUM
I'm curious as to how I would write a for loop that sets each remaining value of a to the value of the preceding element plus 0.1. For example, the second element in the array is the first plus 0.1. I've tried doing
for(i=1; i<=99; i=+0.1)
But I think something is wrong with my initialization of i

Use i to index the array, not to store the value you should put on the array. Remember you can use expressions to access the array, like a[i - 1]
for (i = 1; i < 100; i++)
a[i] = a[i - 1] + 0.1;

int i;
for(i = 0; i < 100; i++)
a[i] = NUM + 0.1 * i;
dont forget to tell the type int !

int i = 0;
for(i = 0; i < 100; i++){
if (i == 0)
a[i] = NUM;
else
a[i] = a[i - 1] + .1;
}
Your array definition includes the step. So your array would run about 1000 times, at 1, 1.1, 1.2, but a[1.1] isn't a valid index of your array. Use i to index the array, and then retrieve the previous value to set the next.

From your question I can understand that this is one of your first program in C/C++, so I think that you need to start from basic things and learn how to do it properly, before doing it elegantly.
http://ideone.com/RGZgXL
for(i = 0; i < ARRAY_SIZE; i++) {
if(i == 0) { // if we are on the first element, set it to NUM
array[i] = NUM;
} else { // otherwise make the sum
array[i] = array[i-1] + STEP;
}
}
In the link you'll find the code and some comments that I hope will help you in understanding it.
Cheers

Related

How to insert same element on DIFFERENT positions of an array (in C)?

I need some assistance in C language.
Assuming I have an array of 10 elements:
int arr[10] = {1,2,999,4,5,999,7,999,9,10};
I want to add the number 1000 at every position where 999 is found without deleting it of course. Or in that case positions where 1000 has to be added: arr[2], arr[5], arr[7]
So my result buffer would be after compiling (of course increased by the amount of positions where 999 has been added):
temp[100] = {1,2,1000,999,4,5,1000,999,7,1000,999,9,10};
Can you help me with that?
You can do this by using conditionals like given below.
//I assume that the arrays are already declared
int i,j;
for(i = 0, j = 0; i < n; i++ , j++){ //here n is the size of the array
if(arr[j] == 999){
temp[i] = 1000;
i++; n++;
temp[i] = 999;
}
else
temp[i] = arr[j];
}
Try this out. This code snippet may not seem so standard but this gives you your desired output...

Need an explanation on how this code works

I'm learning C in college and I don't understand this piece of code that my professor wrote and it has no explanation (I know it adds a random number from 0 to 99 to next array element but don't know how)
srandom(time(NULL));
for(i = 0; i < n; i++)
a[i] = i == 0 ? random() % 100 : a[i-1] + random() % 100;
Though it works as intended, I would just like to understand how it adds numbers to next array element without "+=" or something like that.
You can rewrite it like this:
for(i=0;i<n;i++) {
if (i == 0) {
a[i] = random()%100;
} else {
a[i] = a[i-1] + random()%100;
}
}
So, you see that the value at a[1] is a[0] + random()%100 and so on. The if is needed because when i is 0, you haven't a previous values to use (a[-1] would be an error). The ternary operator condition ? a : b can be read as "if condition is true, a; else b".
You can look at it on this way:
srandom(time(NULL));
for (i = 0; i < n; i++){
if (i == 0){
a[i] = random() % 100; // random number module 100 will give number between 0 and 99 inclusive
}
else{
a[i] = a[i - 1] + random() % 100; // a[i] will get value equal to sum of previous array value a[i - 1] and new random number between 0 and 99
}
}
In for loop you have i++ which is increasing index by one.

Understanding two dimensional arrays

Here's the code and wondering if you can help me understand it.
/* Two dimensional array */
#include <stdio.h>
void main() {
int i, j, sum[2], mean[2];
int mark[3][2] = { { 34, 56}, { 48, 65}, { 53, 59} };
for (j = 0; j < 2; j++)
sum[j] = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
sum[j] = sum[j] + mark[i][j];
}
}
for (j = 0; j < 2; j++)
mean[j] = sum[j] / 3;
printf("Average mark in Mathematics is %d\n", mean[0]);
printf("Average mark in Chemistry is %d\n", mean[1]);
}
My understanding of it so far....
Define data types i, j, sum[2], mean[2] as integers.
Initialising the array....mark is data type int, the array should have 3 rows and 2 columns.
First for loop, j initialised at 0, condition: j has to be less than 2, update: add one onto the value of j. Sum of j = 0.
Also for 2nd loop, i initialised at 0, condition: i has to be less than 3, update: add one onto the value of i.
Similar for the next line that uses the for loop and value j.
I'm a bit confused about the syntax:
sum[j] = sum[j] + mark[i] [j]; does this mean, work out the sum of j and add it to the marks contained in the array displayed as [i] and [j].
After this is completed then similar j loop though not sure how this interacts with the previous loops.
Mean calculated and values printed to the screen.
When I've looked at the worked example...
sum[0] = 0 and sum[1] = 0, I don't really understand why sum[1] is also 0.
Firstly, i=0 and j=0,
sum[0] = sum[0] + mark [0,0]
then j=1
sum[1]=sum[1]+mark[0,1]
then
i=1, j=0
sum[0] = sum[0] + mark [1,0]
then
sum[1] = sum[1]+mark[1,1]
then i = 2, j=0
sum [0] = sum[0]+ mark[2,0]
then
sum[1] = sum[1]+ mark[2,1]
What is confusing me a bit is how the loops are interacting with each other and the values of i and j throughout.
I know that the 2d array would be in a table (that I can't seem to format here).
Would appreciate if anyone could shed some light on this.
sum[j] = sum[j] + mark[i][j]; can be simplified as sum[j] += mark[i][j];. It adds the contents of the cell at row i, column j of the 2D matrix mark to the jth element of array sum.
Accessing an element of a 2D array is written mark[i][j] in C, not mark[i, j].
Note that mark[i, j] is not a syntax error: the expression i, j is a comma expression, evaluating i, then discarding it and evaluating j. It is therefore the same as mark[j] which is not a matrix cell but a reference to the jth row of the 2D matrix.
Nope, an array is in fact a pointer (aka an address). When you define int sum[2], sum is the address of the first element of your array of two integer.
So sum is an int* or int[] (same).
Mark is an 2d array. Mark is in fact an array of array. So Mark contain addresses, and thoses addresses are the beggining of some arrays.
(In fact it can be different in the memory, but the compiler do the work).
Mark is a int**, or an address of address of int.
When you do:
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
sum[j] = sum[j] + mark[i][j];
}
}
It's like if you were saying
for each line i in the array, I want to do:
for each value j in the line, I want to do:
...;
Like this, you work on every "line" (or column, visualize the way you want), and for every "line", you work on every value.

cyclic permutation in O(1) space and O(n) time

I saw an interview question which asked to
Interchange arr[i] and i for i=[0,n-1]
EXAMPLE :
input : 1 2 4 5 3 0
answer :5 0 1 4 2 3
explaination : a[1]=2 in input , so a[2]=1 in answer so on
I attempted this but not getting correct answer.
what i am able to do is : for a pair of numbers p and q , a[p]=q and a[q]=p .
any thoughts how to improve it are welcome.
FOR(j,0,n-1)
{
i=j;
do{
temp=a[i];
next=a[temp];
a[temp]=i;
i=next;
}while(i>j);
}
print_array(a,i,n);
It would be easier for me to to understand your answer if it contains a pseudocode with some explaination.
EDIT : I came to knpw it is cyclic permutation so changed the question title.
Below is what I came up with (Java code).
For each value x in a, it sets a[x] to x, and sets x to the overridden value (to be used for a[a[x]]), and repeats until it gets back to the original x.
I use negative values as a flag to indicate that the value's already been processed.
Running time:
Since it only processes each value once, the running time is O(n).
Code:
int[] a = {1,2,4,5,3,0};
for (int i = 0; i < a.length; i++)
{
if (a[i] < 0)
continue;
int j = a[i];
int last = i;
do
{
int temp = a[j];
a[j] = -last-1;
last = j;
j = temp;
}
while (i != j);
a[j] = -last-1;
}
for (int i = 0; i < a.length; i++)
a[i] = -a[i]-1;
System.out.println(Arrays.toString(a));
Here's my suggestion, O(n) time, O(1) space:
void OrderArray(int[] A)
{
int X = A.Max() + 1;
for (int i = 0; i < A.Length; i++)
A[i] *= X;
for (int i = 0; i < A.Length; i++)
A[A[i] / X] += i;
for (int i = 0; i < A.Length; i++)
A[i] = A[i] % X;
}
A short explanation:
We use X as a basic unit for values in the original array (we multiply each value in the original array by X, which is larger than any number in A- basically the length of A + 1). so at any point we can retrieve the number that was in a certain cell of the original array by array by doing A[i] / X, as long as we didn't add more than X to that cell.
This lets us have two layers of values, where A[i] % X represents the value of the cell after the ordering. these two layers don't intersect through the process.
When we finished, we clean A from the original values multiplied by X by performing A[i] = A[i] % X.
Hopes that's clean enough.
Perhaps it is possible by using the images of the input permutation as indices:
void inverse( unsigned int* input, unsigned int* output, unsigned int n )
{
for ( unsigned int i = 0; i < n; i++ )
output[ input[ i ] ] = i;
}

C: Problem with initializing array values to zero

I'm trying to construct the following 14*14 array i C: [I 0; 0 -I], that is a 7*7 identity matrix upper left, minus the identity lower right and zeros otherwise.
This is the method:
#define DIM 7
double S[2*DIM][2*DIM];
for(i = 0; i < DIM; i++){
for(j = 0; j < DIM; j++){
if(i == j){
S[i][j] = 1.0;
S[i+7][j+7] = -1.0;
}
else{
S[i][j] = 0.0;
}
}
}
This works fine for all the diagonal elements; however, some elements of the array get initialized to crazy values; for example, 13,6 gets initialized to
68111186113812079535019899599437200576833320031036694798491976301968333351950125611739840800974137748034248687763243996679617222196278187875968953700681881752083957666277350377710107236511681624408064.000000
This seems to be happening consistently (at least thrice) to entries 11,13, 12,9, 12,10, 13,12 and 13,6.
Can anybody tell me what's at play here or provide an alternative solution?
Cheers!
EDIT: The weird entries aren't consistent.
EDIT2: Typo: 13,12, not 13,15
Your loop only covers the upper left quadrant, so the off-diagonal elements in the other quadrants are not initialized and contain garbage. Your loop should go up to 2*DIM for each dimension, so that the off-diagonal elements are zeroed, and then your conditional for the diagonal elements should just be a little more complex to decide which value to set the diagonal element to.
Note that [13, 15] is entirely outside of this array!
You can initialize the whole array with zeros, then set only the diagonal
double S[2*DIM][2*DIM] = {0};
for (i = 0; i < DIM; i++) {
s[i][i] = 1;
s[i + DIM][i + DIM] = -1;
}
You are never writing to s[i][j] for i != j and i >= DIM or j >= DIM. If your array has automatic storage (is "local") it contains arbitrary init values.
I would say most of the elements that are outside 7x7 will not be initialized at all unless i == j (diagonal elements).
What do you want to initialize them to?
That's because your not initializing those elements. Here is some better code:
#define DIM 14
double S[DIM][DIM];
for (i = 0; i < DIM; i++) {
for (j = 0; j < DIM; j++) {
if (i == j) {
if (i < 7) {
S[i][j] = 1.0;
} else {
S[i][j] = -1.0;
}
} else {
S[i][j] = 0.0;
}
}
}
You never initialize values with an i or j between DIM + 1 and 2*DIM. So when you look at a value stored in one of those positions, you see whatever was there before that space was accessed by your matrix.

Resources