Some help with C programming questions

Click For Summary
SUMMARY

This discussion focuses on common C programming challenges related to array manipulation. The user seeks assistance with three specific tasks: reversing an array, checking for duplicates in an array, and swapping elements within an array. Key issues identified include improper loop structure in reversing the array, incorrect logic leading to false duplicate detection, and overwriting values during the swap operation. The feedback provided highlights the need for correct loop syntax, proper condition checks, and maintaining value integrity during assignments.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with array manipulation techniques in C
  • Knowledge of control flow statements such as loops and conditionals
  • Experience with variable scope and value assignment in C
NEXT STEPS
  • Review C programming loop structures, specifically the for loop syntax
  • Learn about array traversal and manipulation techniques in C
  • Study methods for detecting duplicates in arrays using efficient algorithms
  • Explore variable assignment and value preservation during element swaps in C
USEFUL FOR

C programmers, computer science students, and anyone looking to improve their skills in array manipulation and debugging in C programming.

fubag
Messages
105
Reaction score
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
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K