Some help with C programming questions

In summary: Instead, try storing the original value of arr[i] in a temporary variable and then assigning that temporary variable to arr[j]. So it would be something like:int temp = arr[i];arr[i] = arr[j];arr[j] = temp;In summary, the conversation discussed various coding exercises and identified errors in the provided code solutions. These included a misplaced semicolon, a mistake in the logic of a for loop, and an issue with overwriting a variable's value. The correct solutions involved removing the extra semicolon, fixing the logic of the for loop, and using a temporary variable to store and swap values.
  • #1
fubag
105
0
I have a few questions:

1. Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.

Given an array a , an int variable n containing the number of elements in a , and two other int variables, k and temp , write a loop that reverses the elements of the array.

Do not use any other variables besides a , n , k , and temp .

I have:

for (temp=n;temp>0; temp--;)

{

for (k=0; k<n;k++)

a[k] = a[temp-1];

}

the compiler yells at me and I don't know what's up...just for everyone to know this is a Codelab assignment.


2. You are given two int variables j and k , an int array zipcodeList that has been declared and initialized, an int variable nZips that contains the number of elements in zipcodeList , and an int variable duplicates .

Write some code that assigns 1 to duplicates if any two elements in the array have the same value, and that assigns 0 to duplicates otherwise.
Use only j , k , zipcodeList , nZips , and duplicates .

I have: duplicates=0;

for (j=0; j<(nZips); j++)
{
for (k=0; k<(nZips); k++)

{ if (zipcodeList[j] == zipcodeList[k])
duplicates=1;

}

}

yet the feedback comes back i am not assigning 0 to duplicates if they don't match...




3. Given an array arr of type int , along with two int variables i and j , write some code that swaps the values of arr and arr[j] . Declare any additional variables as necessary.

I wrote:

int x=j;

arr=arr[j];

arr[j]=arr;

- but the feedback comes that only one variable is changing...I don't understand why..




I know it's a lot but if someone can help me on any of them I would appreciate it a lot!

Thanks!
 
Technology news on Phys.org
  • #2
1. for (temp=n;temp>0; temp--;)

You have a semicolon too many there. There should be exactly two semicolons in a for(a;b;c) clause.

2. This will always return 1, because when j == k then zipcodeList[j]==zipcodeList[k], always.

3. On line two you assign arr[j] to arr. On line three you assign arr to arr[j]. But you destroyed the value of arr on line two, didn't you? You overwrote it with arr[j]. So assigning arr to arr[j] afterwards does nothing.
 
  • #3


I am not an expert in programming, but I will try my best to provide some guidance on these questions.

1. Reversing the elements of an array involves swapping the corresponding elements of the array. In the provided code, it seems like you are assigning the same value to all elements in the array, which is not what we want. Additionally, the for loop should iterate until the middle of the array, not until the end. A possible solution could be:

for (k=0; k<n/2; k++)
{
temp = a[k]; // store the value of the first element in a temporary variable
a[k] = a[n-k-1]; // swap the value of the first element with the last element
a[n-k-1] = temp; // assign the value of the temporary variable to the last element
}

2. For this question, you need to check if there are any duplicates in the array, and if there are, assign 1 to the duplicates variable. Your code seems to be checking for duplicates, but it is not assigning 1 to the duplicates variable. A possible solution could be:

duplicates = 0; // assign 0 to duplicates by default
for (j=0; j<nZips; j++)
{
for (k=j+1; k<nZips; k++) // start the inner loop from j+1 to avoid comparing the same element
{
if (zipcodeList[j] == zipcodeList[k]) // check if two elements have the same value
{
duplicates = 1; // assign 1 to duplicates if there is a duplicate
break; // break out of the inner loop since we have found a duplicate
}
}
}

3. For this question, you are asked to swap the values of arr and arr[j], but in your code, you are assigning the same value to both elements. Additionally, you are not using the variable x that you declared. A possible solution could be:

int temp = arr; // store the value of arr in a temporary variable
arr = arr[j]; // assign the value of arr[j] to arr
arr[j] = temp; // assign the value of the temporary variable to arr[j]

I hope this helps clarify the issues with your code and provides possible solutions. Remember to carefully read the instructions and think
 

FAQ: Some help with C programming questions

1. What is C programming and why is it important?

C programming is a high-level programming language used for creating software and applications. It is important because it allows for efficient and fast execution of programs, making it a popular choice for developing operating systems, games, and other complex software.

2. How do I get started with C programming?

To get started with C programming, you will need to download a C compiler, such as GCC, and a text editor or integrated development environment (IDE) to write and compile your code. There are also many online resources and tutorials available to help you learn the basics of C programming.

3. What are the basic data types in C programming?

The basic data types in C programming are integer (int), character (char), floating-point (float), and double precision (double). There are also modifiers that can be added to these data types to specify their size and range.

4. How do I declare and initialize variables in C programming?

To declare a variable in C programming, you need to specify its data type and name. For example, int num; declares a variable named "num" of type integer. To initialize a variable, you can assign a value to it using the assignment operator (=), such as num = 10;

5. Can you explain the difference between a for loop and a while loop in C programming?

A for loop is a control flow statement that allows you to execute a block of code repeatedly for a specified number of times. It consists of an initialization, condition, and update statement. A while loop, on the other hand, repeatedly executes a block of code as long as the specified condition is true. It only requires a condition statement and does not have an update statement, making it useful for situations where the number of iterations is not known beforehand.

Similar threads

Replies
9
Views
1K
Replies
4
Views
3K
Replies
29
Views
2K
Replies
25
Views
2K
Replies
6
Views
2K
Replies
22
Views
3K
Replies
4
Views
1K
Back
Top